Robert Sim
Robert Sim

Reputation: 1580

Concatenate an IEnumerable of IEnumerables?

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.

Upvotes: 1

Views: 178

Answers (1)

Ronan Thibaudau
Ronan Thibaudau

Reputation: 3603

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);

Upvotes: 5

Related Questions