How can I reduce IEnumerable <IEnumerable <Foo>> to IEnumerable <Foo>?

Sorry for the weird signature. 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 am sure there is a Linq expression for this ...

Sidenote: Lists can be large.

+3
source share
2 answers

As leppie says, you want to Enumerable.SelectMany. The simplest form:

 combined = listOfList.SelectMany(x => x);

It is SelectManycalled in query expressions when you have more than one sentence from, so the alternative would be:

 combined = from x in listOfList
            from y in x
            select y;
+14
source

SelectMany ()

Ok

+2
source

Source: https://habr.com/ru/post/1720671/


All Articles