每n分钟分组一次

我正在玩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分钟要对结果进行分组怎么办?

要按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 } 

//数据每3小时一次

where(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();