13

Is it possible to concatenate a List<List<T>> into a List<T> with a single operation in a way which isn't horrible slow, i.e:

List<List<int>> listOfLists = new List<List<int>>();
List<int> concatenatedList = listOfLists.Something...

?

Andreas Brinck
  • 51,293
  • 14
  • 84
  • 114

2 Answers2

27
listOfLists.SelectMany( l => l );

full line:

List<int> concatenatedList = listOfLists.SelectMany( l => l ).ToList();
Andrey
  • 59,039
  • 12
  • 119
  • 163
0

Something like this:

listOfLists.Aggregate(new int[0], (res, list) => res.Concat(list));
Victor Haydin
  • 3,518
  • 2
  • 26
  • 41