Joa*_*rel 51 linq collections linq-to-objects
我希望能够IEnumerable<IEnumerable<T>>融入IEnumerable<T>(即将所有单个集合合并为一个).该Union运营商只适用于两个集合.任何的想法?
Jar*_*Par 91
尝试
var it = GetTheNestedCase();
return it.SelectMany(x => x);
Run Code Online (Sandbox Code Playgroud)
SelectMany是一个LINQ转换,它实质上是说"For Collection中的每个Item返回一个集合的元素".它会将一个元素转换为多个元素(因此SelectMany).将集合集合分解为平面列表非常有用.
Joe*_*ung 14
var lists = GetTheNestedCase();
return
from list in lists
from element in list
select element;
Run Code Online (Sandbox Code Playgroud)
是使用C#3.0查询表达式语法执行此操作的另一种方法.