I have a class
public class Foo
{
public int ID { get; set; }
}
and I've implemented a LinqEqualityComparer to allow dynamic IEqualityComparer tests for the Except extenion method.
public class LinqEqualityComparer<T> : IEqualityComparer<T>
{
protected Func<T, T, bool> Comparison { get; set; }
public LinqEqualityComparer(Func<T, T, bool> comparison)
{
Comparison = comparison;
}
public bool Equals(T x, T y)
{
return Comparison(x, y);
}
public int GetHashCode(T obj)
{
return obj.GetHashCode();
}
}
I've created the following code to test it:
IEnumerable<Foo> settings = new Foo[]
{
new Foo{ID = 1},
new Foo{ID = 2}
};
IEnumerable<Foo> currentSettings = new Foo[]
{
new Foo{ID = 1},
new Foo{ID = 2},
new Foo{ID = 3}
};
IEqualityComparer<Foo> comparer = new LinqEqualityComparer<Foo>((x, y) => x.ID == y.ID);
IEnumerable<Foo> missing = currentSettings.Except(settings, comparer);
However Foos 1,2 and 3 are all present in the 'missing' variable.
Why does this LinqEqualityComparer not work?