简体   繁体   English

将IEnumerable <IEnumerable <T >>转换为List <List <T >>

[英]Convert IEnumerable<IEnumerable<T>> to List<List<T>>

如何将IEnumerable<IEnumerable<T>>转换为List<List<T>>

How about this ... 这个怎么样 ...

IEnumerable<IEnumerable<int>> input = ...
List<List<int>> nestedList = input.Select(i => i.ToList()).ToList();

Using combination of the List(IEnumerable) constructor and Linq: 使用List(IEnumerable)构造函数和Linq的组合:

List<List<T>> DoIt<T>(IEnumerable<IEnumerable<T>> items)
{
    return new List<List<T>>(items.Select((x) => x.ToList()));
}

可能是这样的:

var test2 = test.ToList().ConvertAll(x => x.ToList());

You might want to think why you are trying to do this. 您可能想要考虑为什么要尝试这样做。 If it is because you have an enumerable of enumerables that have been returned by a LINQ query that you then want to refer to by an index, perhaps LINQ is not the most appropriate way to generate the data structure you want. 如果是因为你有一个可由LINQ查询返回的可枚举的枚举,你想要通过索引引用它,那么LINQ可能不是生成所需数据结构的最合适的方法。 Maybe you should refactor with methods that return lists rather than enumerables. 也许你应该使用返回列表而不是枚举的方法进行重构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM