title: Optimal performance for two dim array to datatable conversion
body: <p>I was looking for an optimized method to convert a square array of type</p>
  
  <p>double[ , ]</p>
  
  <p>to DataTable</p>
  
  <p>I looked up over the internet and couldn't find a way faster than the currently implemented in my method which depends on two nested for loops.</p>
  
  <p>The problem is that the performance is very bad. My input array could be any where from 500 to 15000 elements in each dimension. (Math can be scary)...</p>
  
  <p>This means that it could take ages for the conversion to complete with the current method.</p>
  
  <p>I was thinking about using some unmanaged code or even assembly code to improve the performance but I didn't know how to do it..</p>
  
  <p>My Current code is this...</p>
  
  <pre><code>// helper method to get row as a string array    
  private static string[] GetRowFromArray(double[,] array, int column )
  
  {
  
  int dim = array.GetUpperBound(0) +1;
  
  string[] row = new string[dim];
  
  for(int i = 0; i &lt; dim; i++)
  
  {
  
      row[i] = array[i, column].ToString();
  
  }
  
  return row;
  
  }
  
  
  //the static extension method used to convert the array to datatable
  
  public static DataTable ToDataTable(this double[,] array)
  
  {
  
  DataTable dt = new DataTable();
  
  int rowsCount = array.GetUpperBound(0) + 1;
  
  int colsCount = array.GetUpperBound(1) + 1;
  
  int i = 0;
  
  while (i &lt; colsCount)
  
  {
  
     dt.Columns.Add(); i++;
  
  }
  
  int ii = 0;
  
  while (ii &lt; rowsCount)
  
  {
  
      string[] a = GetRowFromArray(array, ii);
  
      dt.Rows.Add(a);
  
      ii++;
  
  }
  
  return dt;
  
  }
  </code></pre>
  
  <p>Any ideas on how can we optimize this to make it run faster.</p>
  
  <p>Just a side note, the array is square array, so  ColumnsCount = RowsCount</p>
  
  <p>And so many thanks for any help.</p>