Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert IEnumerable<IEnumerable<T>> to List<List<T>>

Tags:

c#

linq

generics

How do I convert a IEnumerable<IEnumerable<T>> to a List<List<T>>?

like image 331
λ Jonas Gorauskas Avatar asked Feb 02 '12 06:02

λ Jonas Gorauskas


3 Answers

How about this ...

IEnumerable<IEnumerable<int>> input = ...
List<List<int>> nestedList = input.Select(i => i.ToList()).ToList();
like image 194
ColinE Avatar answered Oct 14 '22 06:10

ColinE


Using combination of the List(IEnumerable) constructor and Linq:

List<List<T>> DoIt<T>(IEnumerable<IEnumerable<T>> items)
{
    return new List<List<T>>(items.Select((x) => x.ToList()));
}
like image 22
Hand-E-Food Avatar answered Oct 14 '22 05:10

Hand-E-Food


May be like this:

var test2 = test.ToList().ConvertAll(x => x.ToList());
like image 35
manojlds Avatar answered Oct 14 '22 05:10

manojlds