比较两个对象时,有条件地更改GetHashCode()

本文关键字:有条件 GetHashCode 对象 两个 比较 | 更新日期: 2023-09-27 17:54:32

我有两个不同的对象列表,希望根据某些属性的权重获得它们的相似性。最快的方法似乎是实现一个公平的接口,这就是我所做的:

public class CompareEntry : IEquatable<CompareEntry>
{
    public int LeadId { get; set; }
    public int SaleId { get; set; }
    public string Email { get; set; }
    public string PhonePrivate { get; set; }
    public string PhoneMobile { get; set; }
    public string PhoneCompany { get; set; }
    public string FirstName { get; set; }
    public string Name { get; set; }
    public string City { get; set; }
    public string ZipCode { get; set; }
    public string CompanyName { get; set; }
    public bool Equals(CompareEntry other)
    {
        int weight = 0;
        //Check whether the compared object is null.
        if (Object.ReferenceEquals(other, null))
        {
            return false;
        }
        //Check whether the compared object references the same data.
        if (Object.ReferenceEquals(this, other))
        {
            return true;
        }
        if ((this.CheckProperties(this.Email, other.Email) && this.Email == other.Email)
           || (this.CheckProperties(this.PhonePrivate, other.PhonePrivate) && this.PhonePrivate == other.PhonePrivate)
           || (this.CheckProperties(this.PhoneMobile, other.PhoneMobile) && this.PhoneMobile == other.PhoneMobile)
           || (this.CheckProperties(this.PhoneCompany, other.PhoneCompany) && this.PhoneCompany == other.PhoneCompany))
        {
            weight += 100;
        }
        if ((this.CheckProperties(this.Name, other.Name) && this.Name == other.Name)
            || (this.CheckProperties(this.FirstName, other.FirstName) && this.FirstName == other.FirstName))
        {
            weight += 25;
        }
        if ((this.CheckProperties(this.City, other.City) && this.City == other.City)
            || (this.CheckProperties(this.ZipCode, other.ZipCode) && this.ZipCode == other.ZipCode))
        {
            weight += 12;
        }
        if (this.CheckProperties(this.CompanyName, other.CompanyName) && this.CompanyName == other.CompanyName)
        {
            weight += 5;
        }
        return weight > 50;
    }
    public override int GetHashCode()
    {
        unchecked
        {
            int hash = (int)2166136261;
            hash = hash * 16777619 ^ (string.IsNullOrEmpty(Email) ? 0 : Email.GetHashCode());
            //hash = hash * 16777619 ^ (string.IsNullOrEmpty(PhonePrivate) ? 0 : PhonePrivate.GetHashCode());
            //hash = hash * 16777619 ^ (string.IsNullOrEmpty(PhoneMobile) ? 0 : PhoneMobile.GetHashCode());
            //hash = hash * 16777619 ^ (string.IsNullOrEmpty(PhoneCompany) ? 0 : PhoneCompany.GetHashCode());
            //hash = hash * 16777619 ^ (string.IsNullOrEmpty(FirstName) ? 0 : FirstName.GetHashCode());
            //hash = hash * 16777619 ^ (string.IsNullOrEmpty(Name) ? 0 : Name.GetHashCode());
            //hash = hash * 16777619 ^ (string.IsNullOrEmpty(City) ? 0 : City.GetHashCode());
            //hash = hash * 16777619 ^ (string.IsNullOrEmpty(ZipCode) ? 0 : ZipCode.GetHashCode());
            //hash = hash * 16777619 ^ (string.IsNullOrEmpty(CompanyName) ? 0 : CompanyName.GetHashCode());
            return hash;
        }
    }
    private bool CheckProperties(string prop, string otherProp)
    {
        return !string.IsNullOrEmpty(prop) && !string.IsNullOrEmpty(otherProp);
    }
}

问题是,当我重写GetHashCode()方法时,我只得到那些完全相同的人,或者在这种特殊情况下-只有相同的电子邮件。

我怎样才能有条件地检查GetHashCode()方法中的权重,以便我可以使用正确的方法Equals?或者是否有一种方法可以用其他方法进行相似性检查,哪种方法性能更好?

比较两个对象时,有条件地更改GetHashCode()

Equals/GetHashCode不是用来比较"基本相等"的事物的。等式在这里只是一个布尔属性。特别是,使用模糊的"基本相等"方法会导致及物性问题。Object.Equals的文档包含如下要求:

如果(x.Equals(y) && y.Equals(z))返回true,则x.Equals(z)返回true

当你有模糊等式时,它就不成立了。仅仅因为x"很像"yy"很像"z并不意味着x"很像"z

现在你可以做的是有一个只比较电话号码的相等比较器,另一个只比较名字的相等比较器,等等-但这并不能真正得到模糊匹配