Find an item in a list

P

Peter

Hi

I have a list of "Items" which I need to check whether or not it
contains a certain item (based on the item's "id"). What is the "best"
way of doing this?

For example, an Item is declared as the following (actually it's not,
but this is for illustration - Item is a third party class I cannot
change):

public class Item
{
public string Id { get; set; }
public string Name { get; set; }
}


I have a list containing many hundreds of items:

List<Item> itemList = new List<Item>();


I thought of the following 4 methods, a couple using LINQ which I am
only just learning, so my implementations could quite likely be rather
bad. I would appreciate critique of these methods, and opinions on what
is best (fastest, most efficient, prettiest....). As far as I can tell,
my method #1 is fastest.


(1)
A "utility" method:
private bool HasItem(List<Item> itemList, string id)
{
foreach (Item item in itemList)
{
if (item.Id == id)
{
return true;
}
}
return false;
}


(2)
An "extension" method:
public static class ItemListExtension
{
public static bool ContainsItem(this List<Item> itemList, string id)
{
foreach (Item item in itemList)
{
if (item.Id == id)
{
return true;
}
}
return false;
}
}


(3)
LINQ/Lambda:
var a = itemList.FirstOrDefault(itm => itm.Id == findMe);
if (a != null)
{
// list contains the item...
}


(4)
LINQ/query:
var x = (from r in itemList where r.Id == findMe select r).Take(1);
if (x.ToArray().Length > 0)
{
// list contains the item...
}



Thanks,
Peter
 
G

Göran Andersson

Peter said:
Hi

I have a list of "Items" which I need to check whether or not it
contains a certain item (based on the item's "id"). What is the "best"
way of doing this?

For example, an Item is declared as the following (actually it's not,
but this is for illustration - Item is a third party class I cannot
change):

public class Item
{
public string Id { get; set; }
public string Name { get; set; }
}


I have a list containing many hundreds of items:

List<Item> itemList = new List<Item>();


I thought of the following 4 methods, a couple using LINQ which I am
only just learning, so my implementations could quite likely be rather
bad. I would appreciate critique of these methods, and opinions on what
is best (fastest, most efficient, prettiest....). As far as I can tell,
my method #1 is fastest.

All your methods basically do the same; they loop through the items to
look for an id. The LINQ versions has some more overhead, so they will
be somewhat slower.

If you are going to do this more than once, you should put the items in
a dictionary for fast lookup:

Dictionary<string, Item> itemLookup =
new Dictionary<string, Item>(itemList.Count);
foreach (Item item in itemList) {
itemLookup.Add(item.Id, item);
}

Now you can use the ContainsKey method in the dictionary to check if an
item exists. As this is an O(1) operation it will be many hundreds times
faster than looping through the items.
 
C

Cor Ligthert[MVP]

Peter,

Why not simple set it in a test.

For the rest it is just personal preference, especially now you ask the
prettiest.

For me the best method in these cases is mostly the one I remember me the
first.
The differences will be measured in Pico seconds.

Cor
 

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