Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EntitySet<IEnumerable<T>> to IEnumerable<T>

Tags:

linq

I have an

 EntitySet<IEnumerable<T>>

returning from some query and need to cast it to

IEnumerable<T>. 

Can I do it?

like image 214
rudnev Avatar asked Dec 29 '22 21:12

rudnev


2 Answers

EntitySet<IEnumerable<T>> implements IEnumerable<IEnumerable<T>>. So you can do this:

IEnumerable<T> flattenedList = entitySet.SelectMany(e => e);

Looks a little strange, but SelectMany takes a function that gets a "child list" from each item on a list and then concatenates all the child lists together into a single list. In this case, each item on the list is a list, so the lambda is nice and short.

like image 171
Daniel Earwicker Avatar answered Jan 01 '23 15:01

Daniel Earwicker


This looks like a job for SelectMany

like image 36
Amy B Avatar answered Jan 01 '23 15:01

Amy B