marts
marts

Reputation: 79

converting List<List<string[]>> into string [][][] in c#

When creating webservices, in c#, I have found it very useful to pass back jagged arrays, i.e. string[][]

I also found a neat trick to build these in a simple way in my code, which was to create a List and convert it by doing a ToArray() call.

e.g.

public string[][] myws() {
    List<string[]> output = new List<string[]>();
    return output.ToArray();
}

I would like to be able to employ a similar solution, but I can't think how to do something similar with a 3 level jagged array or string[][][], without resorting to loops and such.

Regards Martin

Upvotes: 5

Views: 556

Answers (1)

James Michael Hare
James Michael Hare

Reputation: 38397

You can get there by doing a Select() which converts each inner List<string> to an array using ToArray(), and then converting those results using ToArray():

        var x = new List<List<string[]>>();

        string[][][] y = x.Select(a => a.ToArray()).ToArray();

And so on for as many levels deep as you'd want to go.

Upvotes: 6

Related Questions