How does Except determine if 2 values are the same
I have the following code
var removes = collection.Except(values, comparer).ToList();
var adds = values.Except( collection, comparer).ToList();
foreach (var item in removes)
{
collection.Remove(item);
}
foreach (var item in adds)
{
collection.Add(item);
}
however items that the comparer say are equal are included in the except lists, so to see what is happening I put a break point in the Equals function and its not being called, only the GetHashCode() function
So what criteria is being used to compare the items, is it only if the hashes are different that it calls the equality function?
Edit: the comparer Class and compared class are
public class Lookup
{
public static readonly IEqualityComparer<Lookup> DefaultComparer = new EqualityComparer();
private class EqualityComparer : IEqualityComparer<Lookup>
{
public bool Equals(Lookup x, Lookup y)
{
if (x == null)
return y == null;
else if (y == null)
return false;
else
return x.ID == y.ID
&& x.Category == y.Category
&& x.DisplayText == y.DisplayText
&& MetaData.CollectionComparer.Equals(x.MetaData, y.MetaData);
}
public int GetHashCode(Lookup obj)
{
var rtn = new { obj.ID, obj.Category, obj.DisplayText, obj.MetaData }.GetHashCode();
return rtn;
}
}
[DataMember]
public int ID { get; set; }
[DataMember]
public LookupType Category { get; set; }
[DataMember]
public string DisplayText { get; set; }
[DataMember]
public MetaData[] MetaData { get; set; }
}