How can I reduce IEnumerable<IEnumerable<Foo>> to IEnumerable<Foo>?
-
05-07-2019 - |
Question
Sorry for the weird caption. What I'm trying to achieve is simple:
IEnumerable<IEnumerable<Foo>> listoflist;
IEnumerable<Foo> combined = listoflist.CombineStuff();
Example:
{{0, 1}, {2, 3}} => {0, 1, 2, 3}
I'm positive there is a Linq expression for this...
Sidenote: The lists may be large.
Solution
As leppie says, you want Enumerable.SelectMany
. The simplest form would be:
combined = listOfList.SelectMany(x => x);
In query expressions, SelectMany
is called when you have more than one from
clause, so an alternative would be:
combined = from x in listOfList
from y in x
select y;
OTHER TIPS
SelectMany()
OK
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow