14

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...

?

3
  • 2
    This is called flattening, not concatenation. Commented Jan 19, 2011 at 11:30
  • 2
    Actually, it IS called concatenation, msdn.microsoft.com/en-us/library/ee353462.aspx. Commented Aug 17, 2011 at 14:54
  • To flatten a list of lists, you can concatenate them. Commented Mar 10, 2015 at 12:23

2 Answers 2

28
listOfLists.SelectMany( l => l );

full line:

List<int> concatenatedList = listOfLists.SelectMany( l => l ).ToList();
Sign up to request clarification or add additional context in comments.

Comments

0

Something like this:

listOfLists.Aggregate(new int[0], (res, list) => res.Concat(list));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.