Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enumerate over nested enumerators

I have a variable IEnumerable<IEnumerable<int>>. I'm trying to somehow aggregate it into an IEnumerable<int> which enumerates over all the integers in order. (All the integers from the first set, then all the integers from the second, etc.) I looked into LINQ's aggregate method, but the only examples I found was string concatenation, and I can't figure out how to apply it here.

like image 782
dlras2 Avatar asked Mar 09 '26 19:03

dlras2


1 Answers

You're looking for the SelectMany which can be used to flatten nested IEnumerable<T> structures into an unnested IEnumerable<T>

IEnumerable<IEnumerable<int>> enumerable;
IEnumerable<int> flat = enumerable.SelectMany(x => x);
like image 138
JaredPar Avatar answered Mar 12 '26 11:03

JaredPar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!