45
我希望能夠將IEnumerable<IEnumerable<T>>
合併爲IEnumerable<T>
(即將所有個別集合合併爲一個集合)。 Union
運算符僅適用於兩個集合。任何想法?如何合併Linq中的集合集合
我希望能夠將IEnumerable<IEnumerable<T>>
合併爲IEnumerable<T>
(即將所有個別集合合併爲一個集合)。 Union
運算符僅適用於兩個集合。任何想法?如何合併Linq中的集合集合
嘗試
var it = GetTheNestedCase();
return it.SelectMany(x => x);
是的SelectMany一個LINQ改造基本上說:「對於集合中的每個項目返回一個集合中的元素」。它會把一個元素變成很多(因此SelectMany)。將集合集合分解爲一個單子列表非常棒。
var lists = GetTheNestedCase();
return
from list in lists
from element in list
select element;
是另一種使用C#3.0查詢表達式語法的方法。
LINQ永遠不會讓我感到驚訝,它可以開箱即用。 :) – neminem 2014-04-18 15:29:22