I'm using the Math.Net Numerics package to do some statistics work with regression, etc. Because an assortment of different columns from a particular table may be used as independent variables, I decided to write a method that will create a Matrix from a DataTable directly from a query by creating a double[][] array and passing it to the DenseOfRowArrays() method in MathNet.Numerics. The calling code will ensure that only columns with numeric values will be present in the DataTable.
I got this far following the answers here: C# Cast Entire Array?
public static Matrix<double> DenseOfDataTable(DataTable dt)
{
double[][] arrayFromDataTable = dt.AsEnumerable().Select(row => Array.ConvertAll(row.ItemArray, item => (double)item)).ToArray();
return CreateMatrix.DenseOfRowArrays(arrayFromDataTable);
}
My problem is when running it I get an InvalidCastException on (double)item
with item
showing a value of 0.000 when inspected.