Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Which System.Collections list is best to use for a LIKE search?

Status
Not open for further replies.

kittyyo

Programmer
Oct 17, 2001
89
0
0
AT
Hi all!

Imagine a System.Collections list (e.g. an ArrayList or a HashTable) containing person objects with surnames.

If you wanted to search this list for all people whose surname starts with a specific string (e.g. "Sm" when wanting to find "Smith", "Smithers", ...), which System.Collections list is the best to use?

Of course I could simply use an ArrayList and the following code:

foreach (Person p in list)
{
if p.Surname.StartsWith("Sm")
// here is a matching object
}

However this is slow, is there a better way using e.g. an ordered list or a hash table?
I'd be very much interested in the fastest way ;-) (insertion time is not much of an issue.)

Thanks a lot,
Anne
 
I think that you can put the items in a DataTable and then use the DataTable.Select() method to run a t-sql like query against it, some thing like "WHERE Surname LIKE 'Sm%'".

This link talks about it some about halfway down the page.


----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top