在 LINQ 中展平列表 [英] Flatten List in LINQ

查看:29
本文介绍了在 LINQ 中展平列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 LINQ 查询,它返回 IEnumerable> 但我只想返回 List 所以我想合并我的所有记录在我的 IEnumerable> 中只有一个数组.

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.

示例:

IEnumerable<List<int>> iList = from number in
    (from no in Method() select no) select number;

我想把我所有的结果 IEnumerable> 取到只有一个 List

I want to take all my result IEnumerable<List<int>> to only one List<int>

因此,从源数组:[1,2,3,4][5,6,7]

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

我只想要一个数组[1,2,3,4,5,6,7]

谢谢

推荐答案

尝试 SelectMany()

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

这篇关于在 LINQ 中展平列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆