IEnumerable<IGrouping> to IEnumerable<List>

Doug Peters picture Doug Peters · Mar 15, 2014 · Viewed 16.4k times · Source

So I have this:

IEnumerable<IGrouping<UInt64, MyObject>> groupedObjects = myObjectsResults.GroupBy(x => x.Id);

The question is, how do I turn this result into an IEnumerable<List<MyObject>>?

This is as far as I could take it:

IEnumerable<List<MyObject>> groupedObjects = (myObjectsResults.GroupBy(x => x.Id).SelectMany(group => group).ToList());

which is obviously incorrect. Any ideas?

Answer

L.B picture L.B · Mar 15, 2014
IEnumerable<List<MyObject>> groupedObjects = myObjectsResults.GroupBy(x => x.Id)
                                            .Select(group => group.ToList())
                                            .ToList();