如何合并Linq中的集合集合 [英] How to merge a collection of collections in Linq
本文介绍了如何合并Linq中的集合集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将 IEnumerable< IEnumerable< T>
融入 IEnumerable< T>
(即将所有单独的集合合并为一个)。 Union
运算符仅适用于两个集合。任何想法?
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?
推荐答案
尝试
var it = GetTheNestedCase();
return it.SelectMany(x => x);
SelectMany是一个LINQ转换,它基本上说对于集合中的每个项目返回一个集合的元素。它将一个元素变成许多(因此SelectMany)。它非常适合将集合集合分解为一个平面列表。
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.
这篇关于如何合并Linq中的集合集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文