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

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

问题描述

问题是混乱的,但它更清楚在以下codeS描述:

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天全站免登陆