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
  • 49,374
  • 14
  • 81
  • 112

2 Answers2

27
listOfLists.SelectMany( l => l );

full line:

List<int> concatenatedList = listOfLists.SelectMany( l => l ).ToList();
Andrey
  • 57,904
  • 11
  • 115
  • 158
0

Something like this:

listOfLists.Aggregate(new int[0], (res, list) => res.Concat(list));
Victor Haydin
  • 3,478
  • 1
  • 24
  • 41