按日期范围分组,在每组LINQ中进行计数和排序 [英] Group by date range , count and sort within each group LINQ

查看:354
本文介绍了按日期范围分组,在每组LINQ中进行计数和排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储在对象中的日期集合.这是样本数据.实时地,日期将来自服务电话,我不知道什么日期以及将返回多少日期:

I have a collection of dates stored in my object. This is sample data. In real time, the dates will come from a service call and I will have no idea what dates and how many will be returned:

        var ListHeader = new List<ListHeaderData>
        {
            new ListHeaderData
            {
                EntryDate = new DateTime(2013, 8, 26)
            },
            new ListHeaderData
            {
                EntryDate = new DateTime(2013, 9, 11)
            },
            new ListHeaderData
            {
                EntryDate = new DateTime(2013, 1, 1)
            },
            new ListHeaderData
            {
                EntryDate = new DateTime(2013, 9, 15)
            },
            new ListHeaderData
            {
                EntryDate = new DateTime(2013, 9, 17)
            },
            new ListHeaderData
            {
                EntryDate = new DateTime(2013, 9, 5)
            },
        };

我现在需要按日期范围进行分组,如下所示:

I now need to group by date range like so:

   Today (1) <- contains the date 9/17/2013 and count of 1
   within 2 weeks (3) <- contains dates 9/15,9/11,9/5 and count of 3
   More than 2 weeks (2) <- contains dates 8/26, 1/1 and count of 2

这是我的LINQ语句,不能满足我的需要,但是我认为我在球场上(如果我不在场,请保持友善):

this is my LINQ statement which doesnt achieve what I need but i think i'm in the ballpark (be kind if i'm not):

                   var defaultGroups = from l in ListHeader
                                    group l by l.EntryDate into g
                                    orderby g.Min(x => x.EntryDate)
                                    select new { GroupBy = g };

这是按单个日期分组的,所以我有6个分组,每个分组有1个日期.如何按日期范围,计数和排序对每个组进行分组?预先感谢.

This groups by individual dates, so I have 6 groups with 1 date in each. How do I group by date range , count and sort within each group? Thanking in advance.

推荐答案

介绍数组,其中包含要分组的范围.这是两个范围-今天(零天)和14天(两周):

Introduce array, which contains ranges you want to group by. Here is two ranges - today (zero days) and 14 days (two weeks):

var today = DateTime.Today;
var ranges = new List<int?> { 0, 14 };

现在,根据项目所属的范围将其分组.如果没有合适的范围(所有日期都超过两周),则将使用默认的null范围值:

Now group your items by range it falls into. If there is no appropriate range (all dates more than two weeks) then default null range value will be used:

var defaultGroups = 
      from h in ListHeader
      let daysFromToday = (int)(today - h.EntryDate).TotalDays
      group h by ranges.FirstOrDefault(range => daysFromToday <= range) into g
      orderby g.Min(x => x.EntryDate)
      select g;

更新:添加用于分组的自定义范围:

UPDATE: Adding custom ranges for grouping:

var ranges = new List<int?>();
ranges.Add(0); // today
ranges.Add(7*2); // two weeks
ranges.Add(DateTime.Today.Day); // within current month
ranges.Add(DateTime.Today.DayOfYear); // within current year
ranges.Sort();

这篇关于按日期范围分组,在每组LINQ中进行计数和排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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