Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq: Group by multiple fields using the dot notation

Tags:

c#

linq

I have a nice linq query currently giving me exactly what I need, in this case a list of list<> objects grouped by date, however I need to also group by an ID field and was wondering how I would change the syntax to group by another field. I have tried multiple ways but can't seem to work it out so it still returns me a list of list<> objects grouped by entrydate AND ID fields.

Here is the code currently

var grouped = sales.SalesList.Where(s => !s.Ignore).GroupBy(s => s.EntryDate).Select(grp => grp.ToList()).ToList();

foreach (List<Sale> saleslist in grouped)
{

}

Thanks in advance.

like image 962
Coesy Avatar asked Jul 23 '26 12:07

Coesy


1 Answers

A good way to do this is to group the multiples via an anonymous type:

var grouped = list.GroupBy(x => new { x.a, x.b, x.c });
like image 194
dreadwail Avatar answered Jul 26 '26 00:07

dreadwail