简体   繁体   中英

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

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.

Using a normal list and comparing the proepties via LINQ is not an option as i need decent performance.

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?

EDIT

With help from the comments and answer it seems doing what i want in a single Equals + GetHashCode method is not possible.

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.

This may do the job, but as @Lasse suggests, you need to be careful:

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
}

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