質問

カスタムタイプを2つのList<T>で比較し、Intersect / Exceptメソッドを使用しようとしています。平等はこのタイプの3つのフィールドによって決まります。平等は通常の条件(すべてのフィールドに同じデータを含む)に基づいています。私はもちろんIEqualityComparer<T>を実装しました。私の問題は、HASHCODEが同じではなく、これが私の場合でも真実ではないので、GetHashCode()メソッドが等しくないということです。

平等が複数の条件に基づいているときに2つのカスタムオブジェクトを比較する方法は、交差点/異なるetcなどを使用できるようにしています...?

これは私のコードです:

public bool Equals(ComparableObject x, ComparableObject y)
{
    if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
        return false;

    if (Object.ReferenceEquals(x, y))
        return true;

    if (x.Var1.Equals(y.Var1) && x.Var3.Equals(y.Var3) && !x.Var2.Equals(y.Var2))
        return false;

    if (x.Var1.Equals(y.Var1) && !x.Var3.Equals(y.Var3) && !x.Var2.Equals(y.Var2))
        return true;


    if (!x.Var1.Equals(y.Var1) && x.Var3.Equals(y.Var3) && !x.Var2.Equals(y.Var2))
        return false;

    if (!x.Var1.Equals(y.Var1) && x.Var3.Equals(y.Var3) && x.Var2.Equals(y.Var2))
        return true;

    if (x.Var1.Equals(y.Var1) && !x.Var3.Equals(y.Var3) && x.Var2.Equals(y.Var2))
        return false;

    if (!x.Var1.Equals(y.Var1) && !x.Var3.Equals(y.Var3) && x.Var2.Equals(y.Var2))
        return false;

    if (!x.Var1.Equals(y.Var1) && !x.Var3.Equals(y.Var3) && !x.Var2.Equals(y.Var2))
        return false;


    return x.Var1.Equals(y.Var1) && x.Var1.Equals(y.Var1) && x.Var3.Equals(y.Var3);
}


public int GetHashCode(ComparableObject x)
{
    return obj.Var1.GetHashCode() ^ obj.Var2.GetHashCode()^ obj.Var3.GetHashCode()
}
.

役に立ちましたか?

解決

It is your job to provide such GetHashCode() that the value it returns will be different for objects that are different (in as many cases as possible; you still may return same hash code for non-equal objects), and will always be same for objects that may be equal (in all cases; you may not return different hash code for equal objects).

For instance, if one of the three fields you compare is an int, you can return that field as GetHashCode().

If, however, it's difficult to come up with something clever, you can return a constant, such as 42. This way Equals() will be called for all object pairs, delivering expected results, although in the least performant way.

他のヒント

Not sure if you have other problems with GetHashCode in your underlying types, but this is an example of a custom type and an IEqualityComparer that returns true if only the first two fields are the same. This will allow Except etc. to work on the type.

    public class CustomType
    {
        public int Val1 { get; set; }
        public int Val2 { get; set; }
        public int Val3 { get; set; }
    }

    class CustomTypeComparer : IEqualityComparer<CustomType>
    {
        public bool Equals(CustomType x, CustomType y)
        { return x.Val1 == y.Val1 && x.Val2 == y.Val2; }

        public int GetHashCode(CustomType obj)
        { return obj.Val1.GetHashCode() ^ obj.Val2.GetHashCode(); }
    }

If your properties are not simple types as int, you may want to use Equals() instead of == to compare the objects.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top