Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting Linq List by grouping

Tags:

c#

linq

for reporting purposes i wanna split a list of purchase orders into multiple lists. One list for each purchase address. I know it's possible to group the list by purchase address, but my question is how to split the list by this purchase address into multiple lists and use these multiple list to create individual reporting files.

code:

(from o in orders
group o by new {field1, field2, field3, field4} into og
orderby og.Key.field1
select new ViewClass
{
    purchaseAddress = og.Key.field1,
    product = og.key.field2,
    count = og.count
}).ToList()

question: how to split above list into multiple lists for each purchaseAddress?

like image 328
Luuk Krijnen Avatar asked Dec 15 '22 23:12

Luuk Krijnen


1 Answers

There's a built-in function that I think does what you want. If I assume that your code is assigned to a variable called query then you can write this:

ILookup<string, ViewClass> queryLists = query.ToLookup(x => x.purchaseAddress);

This essentially creates a list of lists that you access like a dictionary, except that each value is a list of zero or more values. Something like:

IEnumerable<ViewClass> someList = queryLists["Some Address"];
like image 82
Enigmativity Avatar answered Dec 22 '22 00:12

Enigmativity