LINQ:按年份和月份组,管理空白个月 [英] Linq: group by year and month, and manage empty months

查看:131
本文介绍了LINQ:按年份和月份组,管理空白个月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的LINQ到组的列表按年,然后按月。

  VAR changesPerYearAndMonth =列表
.GroupBy(改版机=>新建{revision.LocalTimeStamp.Year,revision.LocalTimeStamp.Month})
。选择(组=>新建{GroupCriteria = group.Key,计数= group.Count()} )
.OrderBy(X => x.GroupCriteria.Year)
.ThenBy(X => x.GroupCriteria.Month);



我的电流输出为以下内容:

  2005年,每月1年,计469 
2005年,第5个月,算上487
年份2005,第9个月,算上452
2006年度,月1,算412
年份2006,5月,算上470
...

正如你所看到的,没有任何价值月份,不包括在查询中。我想它们包括,具有以下的输出:

  2005年,每月1年,计469 
2005年,2月份,计数0
2005年,第3个月,算上0
...
2005年,12个月,算上0
2006年新年,1月,算上412 $ b $年b 2006年新年,2月,计数0
...
2006年度,12个月,算上0

在换句话说,我也需要得到空个月。



我能与LINQ查询实现这个?在此先感谢


解决方案

我想你想是这样的:



<$从去年p $ p> VAR changesPerYearAndMonth =
在Enumerable.Range(2005年6)每月
在Enumerable.Range(1,12)
让键=新{=年的年,月=月}
在关键
名单加入修订等于新的{revision.LocalTimeStamp.Year,
revision.LocalTimeStamp.Month}为G
选择新的{GroupCriteria =键,计数= g.Count()};



需要注意的是:




  • 您需要知道你要拿出数据什么年。您可以从另一个查询获取这一点,当然,发现所涉及的最低和最高年 - 或者找到最小并且假设会有什么在未来(我不知道这是否一个有效的假设与否虽然)

  • 这将自动被正确排序


I have the following Linq to group a list by year, then by month.

var changesPerYearAndMonth = list
              .GroupBy(revision => new { revision.LocalTimeStamp.Year, revision.LocalTimeStamp.Month })
              .Select(group => new { GroupCriteria = group.Key, Count = group.Count() })
              .OrderBy(x => x.GroupCriteria.Year)
              .ThenBy(x => x.GroupCriteria.Month);

My current output is the following:

Year 2005, month 1, count 469
Year 2005, month 5, count 487
Year 2005, month 9, count 452
Year 2006, month 1, count 412
Year 2006, month 5, count 470
...

As you can see, months without value, are not included in the query. I would like to include them, having the following output:

Year 2005, month 1,  count 469
Year 2005, month 2,  count 0
Year 2005, month 3,  count 0
...
Year 2005, month 12, count 0
Year 2006, month 1,  count 412
Year 2006, month 2,  count 0
...
Year 2006, month 12, count 0

In other words, I need to get also empty months.

Could I implement this with a Linq query? Thanks in advance

解决方案

I think you want something like this:

var changesPerYearAndMonth =
    from year in Enumerable.Range(2005, 6)
    from month in Enumerable.Range(1, 12)
    let key = new { Year = year, Month = month }
    join revision in list on key
              equals new { revision.LocalTimeStamp.Year, 
                           revision.LocalTimeStamp.Month } into g
    select new { GroupCriteria = key, Count = g.Count() };

Note that:

  • You need to know what years you're trying to come up with data for. You can fetch this from another query, of course, to find the minimum and maximum years involved - or maybe find the minimum and assume there will be nothing in the future (I don't know if this a valid assumption or not though)
  • This will automatically be ordered correctly

这篇关于LINQ:按年份和月份组,管理空白个月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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