简体   繁体   中英

Search DataTable in C#

I have a DataTable with 36000 columns minimum, 20000 rows min. I have a hardcoded list of column names which I need to find in the datatable and perform some calculation on the values for each column.

eg Sum of all rows for a particular column

I was thinking of using a simple foreach to find the column name and datatable.compute method to perform my calculation.

Is there a better way of achieving this? Any help is greatly appreciated.

Thank you.

I am using .Net 4.6 and VS2015

For more performance its good to use parallel methods.

This link has a sample for you to use parallel and it is very Good to use it every where is possible.

At this example you see how much will improve your speed.

You can compare the performance difference between the Datatable.Compute and Foreach.

// Datatable.Compute sample
DataTable table;
table = dataSet.Tables["Orders"];

// Declare an object variable.
object sumObject;
sumObject = table.Compute("Sum(Total)", "EmpID = 5");

Below is a sample of foreach

// Foreach through DataTable
double sum = 0;
dt = ds.Tables["Orders"];
foreach (DataRow dr in dt.Rows)
{
    sum += System.Convert.ToDouble(dr["Total"]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM