What's faster? DataTable iteration or select?

  • Thread starter Thread starter AP
  • Start date Start date
A

AP

To iterate through the rows in a DataTable to find the one that matches the
ID I want, or use the Select functionality of the DataTable? Why?

Adam
 
I've found that the Select statement tends to be quicker, but in most
instances, the difference is negligible. However, there's one case where
it's rather profound...if you reference your column names by name instead of
index. On bigger tables, particularly ones with many columns, if you use
Named based referencing, it's abysmal.

ALso, selecting on a column is based on a key which is a unique value...so
you can go directly to it instead of iterating through unnecessary values.
I believe it's the same concept as a hash table, which finds key values much
faster than iterating over and over a similar structure.

HTH,

Bill
 
So a method like this:

private string GetHUDLineDescription(int theHUDLine) {

foreach (DataRow dr in myDtHudLines.Rows) {

if (((int) dr["HUDLineID"]) == theHUDLine) {

return (string) dr["HUDLineDefaultDescription"];

}

}

return "No HUD Line Description found";

}

would be a lot faster as a select?
 
It depends on the size of everything, if you had two records for instance,
the distinction would not be worth making. However, on larger tables,
particularly considering the ColumnName as an identifier....yes.
AP said:
So a method like this:

private string GetHUDLineDescription(int theHUDLine) {

foreach (DataRow dr in myDtHudLines.Rows) {

if (((int) dr["HUDLineID"]) == theHUDLine) {

return (string) dr["HUDLineDefaultDescription"];

}

}

return "No HUD Line Description found";

}

would be a lot faster as a select?

William Ryan said:
I've found that the Select statement tends to be quicker, but in most
instances, the difference is negligible. However, there's one case where
it's rather profound...if you reference your column names by name
instead
of
index. On bigger tables, particularly ones with many columns, if you use
Named based referencing, it's abysmal.

ALso, selecting on a column is based on a key which is a unique value...so
you can go directly to it instead of iterating through unnecessary values.
I believe it's the same concept as a hash table, which finds key values much
faster than iterating over and over a similar structure.

HTH,

Bill
matches
the
 
Back
Top