1

Suppose I have a linq expression that returns a sequence of IEnumerables. How do I concatenate them into a single flat IEnumerable? Concat seems to be limited to combining only two at a time.

Robert Sim
  • 1,428
  • 11
  • 22
  • Possible duplicate of [Flatten List in LINQ](https://stackoverflow.com/questions/1590723/flatten-list-in-linq) – Gabriel Rainha Feb 07 '18 at 18:36
  • The two questions are very similar and have the same answer, the main difference is I'm asking about the general case, rather than an IEnumerable of Lists case. The distinction only really matters from the perspective of what users might be searching for. – Robert Sim Feb 07 '18 at 19:25

1 Answers1

5

What you're looking for is the SelectMany operator that will flatten an IEnumerable> in a single IEnumerable containing all T without materializing the results

IEnumerable<IEnumerable<string>> MyCollection = getdata();
IEnumerable<string> FlatenedData = MyCollection.SelectMany(item=>item);
Ronan Thibaudau
  • 3,413
  • 3
  • 29
  • 78