每n分钟分组一次

时间:2012-08-21 18:19:11

标签: c# linq

我正在玩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);

enter image description here

每次 5分钟,我需要做什么才能对结果进行分组?

2 个答案:

答案 0 :(得分: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 }

答案 1 :(得分:-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))                                        通过新的{WCGID = Convert.ToInt32组capcityprogressrow(Conversions.GetIntEntityValue( “WCGID”,capcityprogressrow)),WCGName = Conversions.GetEntityValue( “WIRECENTERGROUPNAME”,capcityprogressrow)的ToString(),DUEDATE = Convert.ToDateTime(capcityprogressrow [“数据捕获日期“]),DueTime = capcityprogressrow [”数据捕获时间“]。ToString()}到WCGDateGroup

//对于有顺序的订单

.OrderBy(x => x.WcgName).ThenBy(x => x.DataCapturedDateTime).ThenBy(x => x.DataCapturedTime).ToList();
相关问题