How can I GroupBy execute this LINQ query?

This is my code:

objectList = (from MyObject obj in MyObjects
             select r).ToList();

I would like to return a list of each entry using the "excellent" obj.ID object. How can i do this?

+5
source share
2 answers

This gives you a list of types IGrouping<int, MyObject>(note, I assume it IDhas a type int):

groupedList = (from obj in MyObjects
             group obj by obj.ID into grouped
             select grouped).ToList();
+2
source

It looks like you want ToLookup:

var lookup = MyObjects.ToLookup(x => x.ID);

This allows you to get all values ​​for any particular identifier or iterate over groups. He eagerly appreciated, not the GroupBylazy assessment, which you probably want in this case.

Assuming I understood your request correctly - it’s possible that I didn’t do this ... it would be helpful if you could clarify.

+2
source

All Articles