每n分钟分组 [英] Grouping by every n minutes

查看:79
本文介绍了每n分钟分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在和LINQ一起玩,我想知道按分钟分组很容易,但是我不想每分钟分组,而是每5分钟分组一次.

I'm playing around with LINQ and I was wondering how easy could it be to group by minutes, but instead of having each minute, I would like to group by every 5 minutes.

例如,目前我有:

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分钟将结果分组?

What do I have to do to group my result for each 5 minutes?

推荐答案

要对某物的n进行分组,可以使用以下公式创建存储桶":

To group by n of something, you can use the following formula to create "buckets":

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

这将取总计,将其相除,转换为整数以除去任何小数,然后再次相乘,最后以bucket_size的倍数结束.因此,例如(/是整数除法,因此不需要强制转换):

This will take the total, divide it, cast to integer to drop off any decimals, and then multiply again, which ends up with multiples of bucket_size. So for instance (/ is integer division, so casting isn't necessary):

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 }

这篇关于每n分钟分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆