简体   繁体   English

如何用两种不同的相等组合覆盖Equals和GetHashCode

[英]How to override Equals and GetHashCode with two different combinations of equality

I am creating a collection of my custom class which contains multiple properties. 我正在创建包含多个属性的自定义类的集合。 Below is the class. 下面是课程。

public class OnlineProductHierarchy
{
    public DateTime InsertDate { get; set; }
    public string InsertUserId { get; set; }
    public DateTime AmendDate { get; set; }
    public string AmendUserId { get; set; }
    public string Product { get; set; }
    public string TextField { get; set; }
    public string Value { get; set; }
    public bool IsDefault { get; set; }
}

In order for two object of my class to be considered equal the TextField , Value and Product must be the same or the TextField and Value properties must be the same if IsDefault is true 为了让我的类的两个对象被认为是相等的TextFieldValueProduct必须相同或TextFieldValue的属性必须是相同的,如果IsDefault是真实的

So i have two different way to measure equality and if either are true the objects should be considered equal. 因此,我有两种不同的方法来衡量平等,如果任何一个都是正确的,则应将对象视为平等。 I am doing it this way so i can use a HashSet when creating the collection to remove duplicates. 我这样做是为了在创建集合以删除重复项时可以使用HashSet。

Using a normal list and comparing the proepties via LINQ is not an option as i need decent performance. 使用普通列表并通过LINQ比较各属性不是一个选择,因为我需要不错的性能。

So far i have this code with checks for equality between my first condition but i am unsure how to amend this to include my second quality condition 到目前为止,我有这段代码可以检查我的第一个条件之间的相等性,但是我不确定如何修改它以包括我的第二个质量条件

 public override bool Equals(object obj)
    {
        OnlineProductHierarchy o = obj as OnlineProductHierarchy;

        return o != null && o.Product.ToUpper()
      == this.Product.ToUpper() &&  o.Value.ToUpper() == this.Value.ToUpper()
      && o.TextField.ToUpper() == this.TextField.ToUpper();
    }

    public override int GetHashCode()
    {
        return this.Product.ToUpper().GetHashCode() ^ 
        this.TextField.ToUpper().GetHashCode()
      ^ this.Value.ToUpper().GetHashCode();
    }

This code now correctly identifies duplicates when adding to a hastset for the TextField , Value and Product rule but how can i add to this to include my second rule? 现在,此代码可以正确地将重复项添加到TextFieldValueProduct规则的hastset中,但是如何添加到其中以包括第二条规则?

EDIT 编辑

With help from the comments and answer it seems doing what i want in a single Equals + GetHashCode method is not possible. 在注释和答案的帮助下,似乎不可能在单个Equals + GetHashCode方法中执行我想要的操作。

So my alternative solution as suggested by @Lee was to create two HastSets with different IEqualityComparer implementations and if either of these failed when doing the Add i could identify the duplicate records. 因此,@ Lee建议的我的替代解决方案是创建两个具有不同IEqualityComparer实现的HastSet,并且如果在执行添加i时其中任何一个都失败了,则可以识别重复记录。

This may do the job, but as @Lasse suggests, you need to be careful: 这可以完成工作,但是正如@Lasse所建议的,您需要小心:

public override bool Equals(object obj)
{
    OnlineProductHierarchy o = obj as OnlineProductHierarchy;

    if(o == null) return false;

    return (String.Compare(o.Product, this.Product, true) &&
           String.Compare(o.Value, this.Value, true) &&
           String.Compare(o.TextField, this.TextField, true))
           ||
           (o.IsDefault == this.Isdefault &&
           String.Compare(o.Value, this.Value, true) &&
           String.Compare(o.TextField, this.TextField, true));
}

public override int GetHashCode()
{
    //Not possible using your logic
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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