Get first elements of List <string []> for string [] using LINQ

I have a string array list: List<string[]> myList;

Is there a way to get an array of strings with all elements of string [0]? As List myList for string [] where the elements are myList [string [0]]

I suppose it is something like var result = from x in myList.Take (0) select x

And question 2) is there a way to convert string [] to string [1,] without a for loop? Now I am doing:

for (int i = 0; i < arr.Length; i++)
      range[0, i] = arr[i];

      

I need "this syntax" because I am exporting columns to excel. And I need an object [,] to do range.set_Value(Type, object[,])

.

+3


source to share


3 answers


string[] result = myList.Select(arr => arr.FirstOrDefault()).ToArray();

      

Is this what you are looking for?



Take(n)

only returns an enumerated number at most n

, so obviously wrong.

+6


source


From what I'm gathering from your question, this should suffice:



string[] result = (from item in myList select item.FirstOrDefault()).ToArray());

      

+1


source


For the first part

var temp = myList.Select(q => q.FirstOrDefault());

      

For the second part; I don't think there is a direct operator Linq

to convert one array of dimensions to multidimensional.

+1


source







All Articles