linq Except and custom IEqualityComparer
- by Joe
I'm trying to implement a custom comparer on two lists of strings and use the .Except() linq method to get those that aren't one one of the lists. The reason I'm doing a custom comparer is because I need to do a "fuzzy" compare, i.e. one string on one list could be embedded inside a string on the other list.
I've made the following comparer
`
public class ItemFuzzyMatchComparer : IEqualityComparer
{
bool IEqualityComparer<string>.Equals(string x, string y)
{
return (x.Contains(y) || y.Contains(x));
}
int IEqualityComparer<string>.GetHashCode(string obj)
{
if (Object.ReferenceEquals(obj, null))
return 0;
return obj.GetHashCode();
}
}
`
When I debug, the only breakpoint that hits is in the GetHashCode() method. The Equals() never gets touched. Any ideas?