简体   繁体   中英

Linq: Totals Groups By Month

I have a simple business object:

Crime 
     Date
     PersonsInvolved

I need a list of total PersonsInvolved per month. The data is for 1 year so year doesn't need to be taken into account.

Jan 5
Feb 3
Mar 5

and so on

How is this done in Linq if I have an in memory collection like this:

List<Crime>() crimes;
crimes.GroupBy(c => c.Date.Month)
      .Select(g => new { Month = g.Key, Count = g.Count() });

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)};
List<Crime>() crimes = ...
var crimesPerMonth = crimes.GroupBy(x => x.Date.Month);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM