tags:

views:

286

answers:

8

Suppose you have a collection of classes:

class Foo
{
    public string Bar;
    public string Baz;
}

List<Foo> foolist;

And you want to check this collection to see if another entry has a matching Bar.

bool isDuplicate = false;
foreach (Foo f in foolist)
{
     if (f.Bar == SomeBar)
     {
         isDuplicate = true;
         break;
     }
}

Contains() doesn't work because it compares the classes as whole.

Does anyone have a better way to do this (That works for .NET 2.0).

A: 

fooList.Exists(item => item.Bar == SomeBar)

or with anonymous delegate

fooList.Exists(delegate(Foo item) {return item.Bar == SomeBar;})

Karg
A: 

You probably want to use C5.HashSet, and implement Equals and GetHashCode() for Foo.

ripper234
+4  A: 

Implement the IEqualityComparer<T> interface, and use the matching Contains method.

public class MyFooComparer: IEqualityComparer<Foo> {

   public bool Equals(Foo foo1, Foo foo2) {
      return Equals(foo1.Bar, foo2.Bar);
   }

   public int GetHashCode(Foo foo) {
      return foo.Bar.GetHashCode();
   }
}

Foo exampleFoo = new Foo();
exampleFoo.Bar = "someBar";
if(myList.Contains(exampleFoo, new MyFooComparer())) {
    ...
}
Chris Marasti-Georg
um... you define the IEqualityComparer, but you don't actually use it.
James Curran
A: 

If you need the element, you can also use List.Find() and pass in a delegate that returns true for your definition of a "match" (http://msdn.microsoft.com/en-us/library/x0b5b5bc.aspx).

There's an example of how to define a delegate on that MSDN doc.

Readonly
A: 

If the 'Bar's for your class are unique (a key to class Foo), then you can try implementing a System.Collections.ObjectModel.KeyedCollection. It's pretty simple: just implement the GetKeyForItem() method.

class Foo
{
    public string Bar;
    public string Baz;
}

class FooList : KeyedCollection<string, Foo>
{
    protected override string GetKeyForItem(Foo item)
    {
        return item.Bar;
    }
}

FooList fooList;
Joel Coehoorn
+10  A: 
fooList.Exists(item => item.Bar == SomeBar)

That's not LINQ, but a Lambda expression, but nevertheless, it uses a v3.5 feature. No problem:

fooList.Exists(delegate(Foo Item) { return item.Bar == SomeBar});

That should work in 2.0.

James Curran
A: 

If you override Equals on Foo to make key on Bar, Contains() will work.

Austin Salonen
A: 

If you can use LINQ you can do the following:

bool contains = foolist.Where(f => f.Bar == someBar).Count() != 0;