Copying data from a table into a two-dimensional array Is it possible to remove the inner loop by assigning data.Data.Tables[0].Rows[i].ItemArray directly?

object[,] arr = new object[52784, 21];

for (int i = 0; i &lt; data.Data.Tables[0].Rows.Count; i++)
{
    for (int j = 0; j &lt; data.Data.Tables[0].Columns.Count; j++)
    {
        arr[i, j] = data.Data.Tables[0].Rows[i][j];
    }
}


The reason why I need this code is because, I want to write to data of this dataTable to Excel using Interop which requires a 2-D array as input.
If you need an object[,] your code is as good as i can see it being since a data table is an object[][] at its base (After digging through several different data structures that make up the DataTable.  DataTable has DataRow[] which has DataColumn[] leading to an object[][] after boxing and pulling the actual data value).

Converting between the array structures will require code and while you may be able to do it with linq it would not be as readable as nested for loops (My personal opinion only).

The only enhancements I would suggest would be to store the row and column counts and limit the size of the array to the actual data size. This means you would not have data.Data.Tables[0].X.Count in multiple places and if you found a better way to figure out the counts would only need to change it in 2 places, one for the rows and one for the columns.

int rowCount = data.Data.Tables[0].Rows.Count;
int columnCount = data.Data.Tables[0].Columns.Count;

object[,] arr = new object[rowCount, columnCount];

for (int i = 0; i &lt; rowCount; i++)
{
    for (int j = 0; j &lt; columnCount; j++)
    {
        arr[i, j] = data.Data.Tables[0].Rows[i][j];
    }
}


You could use a foreach loop on the Row.ItemArray and maintain the count but I dont think it would be any faster