I would like to be able to fusion an IEnumerable<IEnumerable<T>>
into IEnumerable<T>
(i.e. merge all individual collections into one). The Union
operators only applies to two collections. Any idea?
Asked
Active
Viewed 1.7k times
53

Joannes Vermorel
- 8,976
- 12
- 64
- 104
2 Answers
96
Try
var it = GetTheNestedCase();
return it.SelectMany(x => x);
SelectMany is a LINQ transformation which essentially says "For Each Item in a collection return the elements of a collection". It will turn one element into many (hence SelectMany). It's great for breaking down collections of collections into a flat list.

JaredPar
- 733,204
- 149
- 1,241
- 1,454
-
5LINQ never fails to surprise me with what it can do out of the box. :) – neminem Apr 18 '14 at 15:29
-
Aahhh....! Same voice, LINQ never disappoints. – Radityo Ardi Dec 28 '22 at 09:23
16
var lists = GetTheNestedCase();
return
from list in lists
from element in list
select element;
is another way of doing this using C# 3.0 query expression syntax.

Joe Chung
- 11,955
- 1
- 24
- 33