LINQ中的展平列表

本文翻译自: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楼

尝试SelectMany()

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;
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值