-3

I have a list of list strings. And I want to convert it to 2D string.

I tried the following the code

IList<IList<string>> string2DList;
string[,] string2D = string2DList.ToArray();

but the above code gives an error on the second line that string2DList.ToArray() is the array of List.

Ajay
  • 33
  • 1
  • 5
  • Please share a [mcve] with sample inputs and expected outputs. – mjwills Jul 20 '21 at 05:56
  • Not sure why this question is downvoted and closed. The reference duplicate [How to convert list of arrays into a multidimensional array](https://stackoverflow.com/questions/9774901/how-to-convert-list-of-arrays-into-a-multidimensional-array) deals with List of 1D array, but my question is about list-of-string-list. – Ajay Jul 20 '21 at 06:49
  • I am not sure. But either way, a [mcve] will help you get an answer faster. – mjwills Jul 20 '21 at 06:49

1 Answers1

-1

Does this help?

string[][] string2D = string2DList.Select(a => a.ToArray()).ToArray();

Bharat
  • 1,192
  • 7
  • 14
  • 2
    I believe it was downvoted because it creates a nested array instead of a multidimensional array. – SomeBody Jul 20 '21 at 06:16
  • But if we understand the difference between [,] and [][]. string[,] is a rectangular array, while string[][] is known as a "jagged array". In this example its not possible to convert to rectangular array as we can never assure that each list has same length of child strings. – Bharat Jul 20 '21 at 06:20
  • Hence why we need a [mcve] @Bharat. – mjwills Jul 20 '21 at 06:26