Take the 2-minute tour ×

I've got a generic list of values. I want to check to see if an Id exists in that generic list.

What's the easiest way to go about this?


List<someCustomObject> mylist = GetCustomObjectList();

int idToCheckFor = 12;

I want to see if 12 exists in any of the custom objects in the list by checking each someCustomObject.Id = idToCheckFor

If a match is found, I'm good to go and my method will return a bool true. I'm just trying to figure out if there's an easy way instead of looping through each item in the list to see if idToCheckFor == someCustomObject.id and setting a variable to true if a match is found. I'm sure there's got to be a better way to go about this.

share|improve this question
So yes I'm going with the LINQ option. But if I were to loop, I guess what, the best way would be to set a bool var to false...and then set it to true when it finds a match and exit out of the loop when it does find the match and return the bool var. –  CoffeeAddict Dec 23 '09 at 5:18
@coffee: If you were looping, the easiest option would be to return true; from within the loop if you found it, then return false; after the loop has completed. –  Adam Robinson Dec 23 '09 at 5:25
For the loop option ... foreach(someCustomObject checkObject in mylist) { if (checkObject.Id == idToCheckFor { return true; } } return false; –  GrayWizardx Dec 23 '09 at 5:25

9 Answers 9

up vote 5 down vote accepted

If you're using .NET 3.5, this is easy using LINQ to objects:

return myList.Any(o => o.ID == idToCheckFor);

Aside from that, looping through is really your only option.

share|improve this answer
Hi Adam .. better query than my version :) (+1), can u point me towards some good linq to objects resources, that would be helpful, Thanks ! :) –  Mahesh Velaga Dec 23 '09 at 5:01
@Mahesh Velaga: Check out the book C# In Depth. –  Jason Dec 23 '09 at 5:03
@Mahesh: The single most helpful resource would be the MSDN page on the Enumerable static class, since it exposes the LINQ-to-objects extension methods. msdn.microsoft.com/en-us/library/system.linq.enumerable.aspx –  Adam Robinson Dec 23 '09 at 5:03
Thanks @Jason and @Adam :) –  Mahesh Velaga Dec 23 '09 at 5:05
Boolean b = myList.Find(obj => obj.id == 12) != null;
share|improve this answer
If you're in 3.5 (which you would need to be to use a lambda), why not use the LINQ method rather than something specific to List<T>? –  Adam Robinson Dec 23 '09 at 4:58
You don't need .net 3.5 to use a lambda. It's a compiler feature. I use lambas all the time when writing .Net 2.0 code with VS2008. It produces rather clean code with native List<T> methods. –  cfern Dec 23 '09 at 9:44

LINQ makes life easier

mylist.Where(x => x.id == idToCheckFor).Any()


share|improve this answer
FYI, it's LINQ, not Link. –  Adam Robinson Dec 23 '09 at 5:01
sorry my bad .. don't know what i was thinking .. thnks for pointing out :) –  Mahesh Velaga Dec 23 '09 at 5:03
 bool found = mylist.Any(p => p.Id == idToCheckFor);
share|improve this answer
bool bExists = myList.Any(x=>x.id == idToCheckFor);
share|improve this answer
if(mylist.Any(Item => Item.Id == idToCheckFor))
share|improve this answer

I think you are using the wrong data structure for this. What you need is:

Dictionary<int, someCustomObject> myDictionary = GetCustomObjectDictionary();

Now you can easily check if the ID exists with fantastic performance.

return myDictionary.ContainsKey(idToCheckFor);
share|improve this answer
This is a difficult assessment to make since you don't know what else he is using the data structure for. –  Jason Dec 23 '09 at 5:01
The dictionary is the way to go if you have a large list of objects, and need to search multiple times. –  ScottS Dec 23 '09 at 5:03
It's entirely possible that a Dictionary is the appropriate data structure, but you are making the assumption that the identifiers are unique. This is likely, but not certain. –  Adam Robinson Dec 23 '09 at 5:05
Yes and you have the ValueCollection which offers much of the same functionality as a List. This means you can have the best of both worlds without having to use LINQ. –  ChaosPandion Dec 23 '09 at 5:06
Well, I inferred from the question that he expects the items to have unique IDs. Wouldn't be very good IDs if they weren't unique. –  ChaosPandion Dec 23 '09 at 5:08

return myList.Exists(item => item.Id == idToCheckFor);

For linq resources you can check 101 linq samples

share|improve this answer

Use LINQ to Objects. Something like the following:

var result = from l in mylist
             where l.id = 12
             select l;

return result != null;
share|improve this answer
-1. result is a query, not an object. It would never be null. –  Adam Robinson Dec 23 '09 at 5:00
Also = is an assignment operator, not an operator to test for equality. –  Jason Dec 23 '09 at 5:02

Your Answer


