bel*_*hev 1 .net c# comparison performance gethashcode
我有两个不同的对象列表,并希望根据某些属性的权重获得它们的相似性.最快的方式似乎是实现IEquatable接口,这就是我所做的:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,当我覆盖GetHashCode()方法时,我只会获得完全相同或在这种特殊情况下的那些 - 只有相同的电子邮件.
我怎样才能在GetHashCode()方法中有条件地检查权重,这样我才能使用正确的方法Equals?或者有没有办法用其他方式进行相似性检查,哪个性能好?
Equals/ GetHashCode不是为了比较"大多数相等"的东西.在这种情况下,Equality只是一个布尔属性.特别是,具有模糊的"大多数相等"方法会导致传递性问题.文件Object.Equals包括这个要求:
如果
(x.Equals(y) && y.Equals(z))返回true,则x.Equals(z)返回true.
当你有模糊的平等时,这根本不成立.仅仅因为x"非常喜欢" y并且y"非常喜欢" z并不意味着x"非常喜欢" z.
现在你可以做的是有一个相等比较器,它只比较电话号码,另一个只比较名称的相等比较器等等 - 但这并不能真正让你模糊匹配.
| 归档时间: |
|
| 查看次数: |
198 次 |
| 最近记录: |