Selecting a multi-dimensional array in LINQ

Posted by mckhendry on Stack Overflow See other posts from Stack Overflow or by mckhendry
Published on 2010-04-01T18:48:32Z Indexed on 2010/04/01 18:53 UTC
Read the original article Hit count: 426

Filed under:
|
|

I have a task where I need to translate a DataTable to a two-dimensional array. That's easy enough to do by just looping over the rows and columns (see example below).

private static string[,] ToArray(DataTable table)
{
    var array = new string[table.Rows.Count,table.Columns.Count];

    for (int i = 0; i < table.Rows.Count; ++i)
        for (int j = 0; j < table.Columns.Count; ++j)
            array[i, j] = table.Rows[i][j].ToString();

    return array;
}

What I'd really like to do is use a select statement in LINQ to generate that 2D array. Unfortunately it looks like there is no way in LINQ to select a multidimensional array. Yes, I'm aware that I can use LINQ to select a jagged array, but that's not what I want.

Is my assumption correct, or is there a way to use LINQ to select a multi-dimensional array?

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ