[英]LINQ GroupBy anonymous type and select timestamp that is within +/- one minute
我有一個答案列表,我通過將FormId,ProductInstance和最后由TimeStamp匹配來進行分組,如下所示:
var groups = answersForCustomerList.GroupBy(x => new { x.FormId, x.ProductInstance, x.TracingData.Timestamp });
這給了我這樣的團體:
FormId = {97748e5e-be41-4d53-9ef8-f0dc150e58b6}, ProductInstance = "kontot", Timestamp = {2016-12-20 16:00:00}
FormId = {87dd356b-2770-4427-89d2-7503ee80bb1c}, ProductInstance = "Lån", Timestamp = {2016-12-20 16:02:00}
FormId = {87dd356b-2770-4427-89d2-7503ee80bb1c}, ProductInstance = "Lån", Timestamp = {2016-12-20 16:01:00}
FormId = {97748e5e-be41-4d53-9ef8-f0dc150e58b6}, ProductInstance = "Konto", Timestamp = {2016-12-20 15:00:00}
FormId = {87dd356b-2770-4427-89d2-7503ee80bb1c}, ProductInstance = "Lån", Timestamp = {2016-12-20 15:59:00}
但是我需要做的事情似乎無法弄清楚如何將+/- 1分鍾以內的時間戳分組到同一組中。 因此,具有相同FormId和ProductInstance但具有TimeStamp 16:01:00或15:59:00的答案與16:00:00組處於同一組。
我設法得到了我想要的東西:
var groups = answersForCustomer
.GroupBy(x =>
{
var stamp = x.TracingData.Timestamp;
stamp = stamp.AddMinutes(-(stamp.Minute % 2));
stamp = stamp.AddMilliseconds(-stamp.Millisecond - 1000 * stamp.Second);
return new { stamp, x.FormId, x.ProductInstance };
});
這使我可以提前2分鍾到達小組,感謝所有答案!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.