468,780 Members | 2,324 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,780 developers. It's quick & easy.

DataTable.Select vs DataTable.rows.Find vs foreach

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

May 17 '07 #1
1 28600
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Dave" <a_*************@hotmail.comwrote in message
news:04**********************************@microsof t.com...
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

May 17 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Luc | last post: by
1 post views Thread by Sunny | last post: by
reply views Thread by Raj Chudasama | last post: by
1 post views Thread by Diffident | last post: by
7 posts views Thread by wk6pack | last post: by
3 posts views Thread by Jeff | last post: by
4 posts views Thread by ArunDhaJ | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.