12

我在玩 LINQ,我想知道按分钟分组有多容易,但我不想每分钟分组一次,而是每 5 分钟分组一次。

例如,目前我有:

var q = (from cr in JK_ChallengeResponses
        where cr.Challenge_id == 114
        group cr.Challenge_id 
            by new { cr.Updated_date.Date, cr.Updated_date.Hour, cr.Updated_date.Minute } 
            into g 
        select new {
          Day = new DateTime(g.Key.Date.Year, g.Key.Date.Month, g.Key.Date.Day, g.Key.Hour, g.Key.Minute, 0),
          Total = g.Count()
        }).OrderBy(x => x.Day);

在此处输入图像描述

每5 分钟对我的结果进行分组需要做什么?

4

2 回答 2

14

n对某事物进行分组,您可以使用以下公式来创建“桶”:

((int)(total / bucket_size)) * bucket_size

这将取总数,除以,转换为整数以去掉任何小数,然后再次相乘,最终得到bucket_size. 例如(/是整数除法,所以不需要强制转换):

group cr.Challenge_id
    by new { cr.Updated_Date.Year, cr.Updated_Date.Month, 
             cr.Updated_Date.Day, cr.Updated_Date.Hour,
             Minute = (cr.Updated_Date.Minute / 5) * 5 }
于 2012-08-21T18:23:26.367 回答
-1

//数据每 3 小时来一次

其中 (Convert.ToDateTime(capcityprogressrow["Data Captured Date"].ToString()).Date != DateTime.Now.Date || (Convert.ToDateTime(capcityprogressrow["Data Captured Date"].ToString()).Date == DateTime.Now.Date && (Convert.ToInt16(capcityprogressrow["Data Captured Time"])) % 3 == 0)) group capcityprogressrow by new { WCGID = Convert.ToInt32(Conversions.GetIntEntityValue("WCGID", capcityprogressrow )), WCGName = Conversions.GetEntityValue("WIRECENTERGROUPNAME", capcityprogressrow).ToString(), DueDate = Convert.ToDateTime(capcityprogressrow["Data Captured Date"]), DueTime = capcityprogressrow["Data Captured Time"].ToString() } 进入 WCGDateGroup

// 对于顺序递增的 orderby

.OrderBy(x => x.WcgName).ThenBy(x => x.DataCapturedDateTime).ThenBy(x => x.DataCapturedTime).ToList();
于 2017-02-09T14:51:02.913 回答