This is my code :
objectList = (from MyObject obj in MyObjects
select r).ToList();
I'd like to return the list of each record with a "distinct" obj.ID. How can I do this?
This gives you the list of type IGrouping<int, MyObject>
(note, I assume that ID
has type int
):
groupedList = (from obj in MyObjects
group obj by obj.ID into grouped
select grouped).ToList();
It sounds like you might want ToLookup
:
var lookup = MyObjects.ToLookup(x => x.ID);
That lets you fetch all the values for any particular ID, or iterate over the groupings. It's eagerly evaluated, rather than GroupBy
's lazy evaluation, which is probably what you want in this case.
That's assuming I understood your request correctly - it's entirely possible that I didn't... it would be helpful if you could clarify.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With