Converting a Datatable to an Array of Doubles

I have a DataTable as a result of reading SQL. A typical example contains 100 rows and 16 columns. I need to repeatedly iterate over a table for extracting values ​​for calculations (application - gradient descent of machine learning). I am trying to speed up the calculations and avoid using the values ​​read from the DataTable every time they are used. Is there a way to create a new 2D-indexed array by excluding the entire DataTable once, and therefore eliminate the need to call Convert.ToDouble 1000x100x16 = 1,600,000 times. Ideally for reading code, I would like to keep the data link as ["colname", row]

Current implementation:

for(i=0;i<1000;i++)
  foreach (DataRow row in dt.Rows)
    calculation = Convert.ToDouble(row["col1"])....
+4
source share
4

:

using System;
using System.Linq;
using System.Data;    
DataTable db = <some table>;
double[][] arrayOfDoubles = db.AsEnumerable().Select(x => new [] { Convert.ToDouble(x["SomeColumn"]), Convert.ToDouble(x["SomeColumn"]), ... }).ToArray();

DbNull

+3

:

public class Dto // you can chose better name for dto class
{
   public double Column1 { get; set; }
   // other properties go here
}

, :

var map = dt.AsEnumerable()
            .Select((r,i) => new { 
                RowIndex = i,
                Value = new Dto { 
                    Column1 = r.Field<double>("col1") 
                    // parse other columns here
                }                    
             }))
            .ToDictionary(x => x.RowIndex, x => x.Value);

map[rowIndex].Column1
+2

dt.Rows.Select(r => Convert.ToDouble(r["col1"])).ToArray()   double , DataTable. , , istelf, .

, System.Linq System.Data.DatasetExtensions, .

, :

var arr = dt.Rows.Select(r => Convert.ToDouble(r["col1"])).ToArray();

for(i=0;i<1000;i++)
    for(j=0;j<arr.Length;j++)
    {
        calculation = arr[j] /* your operation here */;
        //dt.Rows[j] is also available if needed at any time
    }   
0
source

You can create a class to hold your shapes, and perhaps even put some of their calculations.

List<MyCalculationOject> calculationOjects = dt.AsEnumerable().Select(
    row => new MyCalculationOject {
        Figure1 = row.Field<double>("figure1_Col"), 
        Figure2 = row.Field<double>("figure2_Col"), 
        ....})).ToList();

public class MyCalculationOject
{
    public double Figure1 {get;set;}
    ...

    public double SomeBasicCalculation() {..}
}
0
source

Source: https://habr.com/ru/post/1674751/


All Articles