tags:

views:

420

answers:

5

I have a list which contains a collection of objects.

How can I search for an item in this list where object.Property == myValue

A: 
item = objects.Find(obj => obj.property==myValue);
Jonas Elfström
+17  A: 

You have a few options:

  1. Using Enumerable.Where:

    list.Where(i => i.Property == value).FirstOrDefault();       // C# 3.0+
    
  2. Using List.Find:

    list.Find(i => i.Property == value);                         // C# 3.0+
    list.Find(delegate(Item i) { return i.Property == value; }); // C# 2.0+
    

Both of these options return default(T) (null for reference types) if no match is found.

As mentioned in the comments below, you should use the appropriate form of comparison for your scenario:

  • == for simple value types or where use of operator overloads are desired
  • object.Equals(a,b) for most scenarios where the type is unknown or comparison has potentially been overridden
  • string.Equals(a,b,StringComparison) for comparing strings
  • object.ReferenceEquals(a,b) for identity comparisons, which are usually the fastest
Drew Noakes
Forgot to mention I'm using .net v2
JL
4 upvotes and still using the assignment operator to compare values. Hmm....
Peter van der Heijden
@Peter -- haha, nice one. It's early here and the compiler in my brain is off duty :)
Drew Noakes
You should compare using `Equals` unless you know `==` is valid for the particular type you're comparing. `==` will most often compare reference identity, which has a good chance of not having desirable semantics.
Joren
I generally use `object.Equals(a,b)` when I don't know the type (most times you're looking in a list, you do know the type) as this takes type-specific comparison into account and deals neatly with nulls, though the exception to this rule is for string comparisons, for which the programmer should indicate whether it's an ordinal or culture-sensitive comparison (via `string.Equals(a,b,StringComparison)`.
Drew Noakes
I didn't really mean situations where you actually don't know the type (indeed `object.Equals` is a good choice there). My intent was more to say that by default you should use `Equals`, and only if you are *certain* `==` is correct (e.g. immutable types that overload it, or reference types that don't overload it but for which you want reference equality) should you consider using it.
Joren
@Joren - Agreed. I think we're both on the same page :)
Drew Noakes
+5  A: 

What is wrong with List.Find ??

I think we need more information on what you've done, and why it fails, before we can provide truly helpful answers.

abelenky
You're right, found List.Find, works like a charm.
JL
If you are using .NET 3.0 or later, you should favor the LINQ way because LINQ works on any IEnumerable and IEnumerable<T>. The Find-Method is specific for List<T> and cannot help you once you have to deal with different collections.
Oliver Hanappi
+1  A: 
var myItem = myList.Find(item => item.property == "something");
shahkalpesh
A: 

For .NET 2.0:

list.Find(delegate(Item i) { return i.Property == someValue; });
oreon