本文翻译自:Flatten List in LINQ
I have a LINQ query which returns IEnumerable<List<int>>
but i want to return only List<int>
so i want to merge all my record in my IEnumerable<List<int>>
to only one array. 我有一个LINQ查询,该查询返回IEnumerable<List<int>>
但我只想返回List<int>
所以我想将我的IEnumerable<List<int>>
所有记录合并到一个数组中。
Example : 范例:
IEnumerable<List<int>> iList = from number in
(from no in Method() select no) select number;
I want to take all my result IEnumerable<List<int>>
to only one List<int>
我想将我所有的结果IEnumerable<List<int>>
带到一个List<int>
Hence, from source arrays: [1,2,3,4] and [5,6,7] 因此,从源数组:[1,2,3,4]和[5,6,7]
I want only one array [1,2,3,4,5,6,7] 我只想要一个数组[1,2,3,4,5,6,7]
Thanks 谢谢
#1楼
参考:https://stackoom.com/question/6FOP/LINQ中的展平列表
#2楼
var result = iList.SelectMany( i => i );
#3楼
像这样?
var iList = Method().SelectMany(n => n);
#4楼
如果您具有List<List<int>> k
,则可以执行
List<int> flatList= k.SelectMany( v => v).ToList();
#5楼
iList.SelectMany(x => x).ToArray()
#6楼
With query syntax: 使用查询语法:
var values =
from inner in outer
from value in inner
select value;