views:

68

answers:

3

How can I convert a List<List<string>> to a List<string[]> in C# in a concise way using delegates/LINQ?

+6  A: 

I believe this will do it:

var result = (from list in listOfLists select list.ToArray()).ToList()
Dean Harding
or `var result = listOfList.Select(list => list.ToArray()).ToList()`, which is just the other way of writing it.
zneak
i've been doing it this way all the time
GerManson
+2  A: 
List<List<string>> listOfLists = new List<List<string>>();

listOfLists.Add(new List<string>(){ "bla", "foo", "bar"});
listOfLists.Add(new List<string>() { "alfa", "beta", "gama" });
listOfLists.Add(new List<string>() { "earth", "moon", "sun" });
listOfLists.Add(new List<string>() { "cat", "dog", "owl" });

List<string[]> listOfArrays = new List<string[]>();

listOfArrays = listOfLists.Select(l => l.ToArray()).ToList();
Leniel Macaferi
+3  A: 
var lists = new List<List<string>>();
List<string[]> arrays = lists.ConvertAll(l => l.ToArray());
kekekela