DataTable.Select vs DataTable.rows.Find vs foreach

D

Dave

Hello,
What is the best way (performance wise) to search for a row in a table?
Let say I have a column named "col1" and what to get the row that the value
of "col1" is "It is me"...
I can set this column as the primary key to use the find method if it worths
it.
So my options are:
1- using the DataTable.Select method and provide a criteria like "col1 = 'It
is me'"
2- creating a primary with "col1" and use the DataTable.Rows.Find("It is
me")
3- using a foreach statement to loop thru each row of the table and find the
row where the value of "col1" = "It is me"

any other better ways?

Thanks,
David
 
N

Nicholas Paldino [.NET/C# MVP]

Dave,

If it weren't for the primary key, I would say it would depend on the
selectivity of your filter. For filters that are not highly selective
(which return a large number of rows), I would say that looping through the
records is probably the fastest (of course, your own tests should confirm
this, as we might have different definitions of "large" and "selective").

However, you did indicate that you had the option of using a primary
key, which indicates that there is a high selectivity for this query (since
each value would be unique). In this case, I think that creating the
primary key and then calling Find would be fastest.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top