开发者

Flatten List in LINQ

开发者 https://www.devze.com 2022-12-09 01:44 出处:网络
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 IEnu开发者_JAVA技巧merable<List<int>>

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 IEnu开发者_JAVA技巧merable<List<int>> to only one array.

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>

Hence, from source arrays: [1,2,3,4] and [5,6,7]

I want only one array [1,2,3,4,5,6,7]

Thanks


Try SelectMany()

var result = iList.SelectMany( i => i );


With query syntax:

var values =
from inner in outer
from value in inner
select value;


iList.SelectMany(x => x).ToArray()


If you have a List<List<int>> k you can do

List<int> flatList= k.SelectMany( v => v).ToList();


Like this?

var iList = Method().SelectMany(n => n);
0

精彩评论

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

关注公众号