linq Except and custom IEqualityComparer

Posted by Joe on Stack Overflow See other posts from Stack Overflow or by Joe
Published on 2010-03-23T15:11:55Z Indexed on 2010/03/23 15:13 UTC
Read the original article Hit count: 464

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about custom