开发者

How to enumerate over nested enumerators

开发者 https://www.devze.com 2023-01-21 18:34 出处:网络
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

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.


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

精彩评论

暂无评论...
验证码 换一张
取 消