How can I convert a List<List<string>>
to a List<string[]>
in C# in a concise way using delegates/LINQ?
views:
68answers:
3
+6
A:
I believe this will do it:
var result = (from list in listOfLists select list.ToArray()).ToList()
Dean Harding
2010-08-04 01:50:06
or `var result = listOfList.Select(list => list.ToArray()).ToList()`, which is just the other way of writing it.
zneak
2010-08-04 01:57:01
i've been doing it this way all the time
GerManson
2010-08-04 03:17:59
+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
2010-08-04 01:58:30
+3
A:
var lists = new List<List<string>>();
List<string[]> arrays = lists.ConvertAll(l => l.ToArray());
kekekela
2010-08-04 01:58:46