我有一个简单的业务对象:
Crime
Date
PersonsInvolved
我需要一个每月总人数列表。这些数据是1年,所以不需要考虑这一年。
Jan 5
Feb 3
Mar 5
等等
如果我有这样的内存集合,如何在Linq中完成:
List<Crime>() crimes;
答案 0 :(得分:10)
crimes.GroupBy(c => c.Date.Month)
.Select(g => new { Month = g.Key, Count = g.Count() });
答案 1 :(得分:4)
var q = from i in crimes
group i by i.Date.ToString("MMM") into grp
select new {Month = grp.Key, Count = grp.Sum(i => i.Persons)};
答案 2 :(得分:0)
List<Crime>() crimes = ...
var crimesPerMonth = crimes.GroupBy(x => x.Date.Month);