9

Possible Duplicate:
Linq: List of lists to a long list

I have convert it using LINQ.
List<List<string>> to List<string>.
If the leaves overlap one. Must be In one line.

Community
  • 1
  • 1
Rafal T
  • 339
  • 1
  • 2
  • 14
  • 1
    Can you provide an example? I'm not sure if I understand what you need. – SWeko Jul 07 '11 at 11:48
  • "If the leaves overlap one" what do you mean by that? Do you want to simply flatten the list, or do you only want distinct elements, or something entirely different? – CodesInChaos Jul 07 '11 at 11:51
  • SelectMany is what exactly I was looking for. Thank you all. – Rafal T Jul 07 '11 at 11:54
  • When I typed differently I found even otherwise. http://stackoverflow.com/questions/462879/convert-listlistt-into-listt-in-c I apologize for repeating questions – Rafal T Jul 07 '11 at 11:59

3 Answers3

17
input.SelectMany(l => l).Distinct().ToList();
Arcturus
  • 26,677
  • 10
  • 92
  • 107
5

Your question is a bit under specified.

input.SelectMany(list=>list).ToList()

This puts all strings that are part of any list into the result list. If you need only unique elements add .Distinct between the SelectMany and the ToList

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
1
List<List<string>> listOfLists = new List<List<string>>();
List<string> flattenedList = ListOfLists.SelectMany(x => x).ToList();
Ben Robinson
  • 21,601
  • 5
  • 62
  • 79