如何将具有相同类型项目的列表列表合并为单个项目列表? [英] How to merge a list of lists with same type of items to a single list of items?

查看:24
本文介绍了如何将具有相同类型项目的列表列表合并为单个项目列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题令人困惑,但如以下代码所述,它更清楚:

The question is confusing, but it is much more clear as described in the following codes:

   List<List<T>> listOfList;
   // add three lists of List<T> to listOfList, for example
   /* listOfList = new {
        { 1, 2, 3}, // list 1 of 1, 3, and 3
        { 4, 5, 6}, // list 2
        { 7, 8, 9}  // list 3
        };
   */
   List<T> list = null;
   // how to merger all the items in listOfList to list?
   // { 1, 2, 3, 4, 5, 6, 7, 8, 9 } // one list
   // list = ???

不确定是否可以使用 C# LINQ 或 Lambda?

Not sure if it possible by using C# LINQ or Lambda?

本质上,我如何连接或展平"列表列表?

Essentially, how can I concatenate or "flatten" a list of lists?

推荐答案

使用 SelectMany 扩展方法

Use the SelectMany extension method

list = listOfList.SelectMany(x => x).ToList();

这篇关于如何将具有相同类型项目的列表列表合并为单个项目列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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