简体   繁体   中英

LINQ Distinct not working as expected

I have the following simple class

public class Person : IEquatable<Person>
{
    public bool Equals(Person other) 
    {
        return Name.Equals(other.Name, StringComparison.InvariantCultureIgnoreCase);
    }

    public override int GetHashCode()
    {
        return Name.GetHashCode();
    }

    public Person(string name)
    {
        Name = name;
    }
    public string Name { get; set; }
}

Now I am creating an array of persons, calling distinct on them and pass the default Equality Comparer, which is the one implemented by IEquatable<Person>

var persons = new[] {new Person("foo"), new Person("Foo"), new Person("bar"), new Person("Bar")};
persons.Distinct(EqualityComparer<Person>.Default);

When I inspect the distincted persons, I am expecting an IEnumerable<Person> containing foo, bar . However the contents are foo, Foo, bar, Bar

When I initialize the List with foo, foo, bar, bar the result is as expected. So it seems to me as if StringComparison.InvariantCultureIgnoreCase in the Person.Equals method is ignored.

Has anyone an idea?

You need to make GetHashCode() get a case-* in *sensitive Hash Code at the moment, it will be a different hash code for upper and lower case Name 's.

For example:

public override int GetHashCode() 
{ 
    return Name.ToUpperInvariant().GetHashCode(); 
} 

您的GetHashCode()将返回应被视为相同的对象的不同哈希码。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM