Reputation: 218
I have a datatable which contains rows like
123, 1, ABC, 2, 21.50, 36.33
123, 1, ABC, 2, 22.67, 0.00
456, 1, ABC, 2, 101.02, 53.92
456, 1, ABC, 2, 0.00, 0.00 ...
I want to loop through the datatable and process each ID (column 1).
The code below worked fine but it is very slow. It took 15 minutes to iterate through 200,000 rows. Is there a way to boost the performance?
if (dt.Rows.Count > 0)
{
DataView distinctDv = new DataView(dt);
DataTable distinctDt = distinctDv.ToTable(true, "ID");
foreach (DataRow distinctRow in distinctDt.Rows)
{
DataView dv = DataView(dt);
dv.RowFilter = "ID = " + distinctRow["ID"];
foreach (DataRowView drv in dv)
{
//Logic
}
}
Upvotes: 1
Views: 1723
Reputation: 8150
You could use LINQ to group your rows by ID, which should only loop through the DataTable once, instead of the once for each key that your RowFilter solution is likely doing (forgive my C#, I've been in VB.NET land for too long!):
var grouped = (
from row in dt.AsEnumerable()
group row by row.Field<int>("ID") into g
select g
);
foreach (var grp in grouped)
{
Console.WriteLine("ID = {0}", grp.Key);
foreach (var row in grp)
{
// Logic - row will be the DataRow
}
}
Upvotes: 1