質問

次のクラスがあります:

public class SupplierCategory : IEquatable<SupplierCategory>
{
    public string Name { get; set; }
    public string Parent { get; set; }

    #region IEquatable<SupplierCategory> Members

    public bool Equals(SupplierCategory other)
    {
        return this.Name == other.Name && this.Parent == other.Parent;
    }

    #endregion
}

public class CategoryPathComparer : IEqualityComparer<List<SupplierCategory>>
{
    #region IEqualityComparer<List<SupplierCategory>> Members

    public bool Equals(List<SupplierCategory> x, List<SupplierCategory> y)
    {
        return x.SequenceEqual(y);
    }

    public int GetHashCode(List<SupplierCategory> obj)
    {
        return obj.GetHashCode();
    }

    #endregion
}

そして、私は次のlinqクエリを使用しています:

CategoryPathComparer comparer = new CategoryPathComparer();
List<List<SupplierCategory>> categoryPaths = (from i in infoList
                                                          select
                                                            new List<SupplierCategory>() { 
                                                             new SupplierCategory() { Name = i[3] },
                                                             new SupplierCategory() { Name = i[4], Parent = i[3] },
                                                             new SupplierCategory() { Name = i[5], Parent = i[4] }}).Distinct(comparer).ToList();

しかし、次のコードが示すように、distinctは私がやりたいことをしません:

comp.Equals(categoryPaths[0], categoryPaths[1]); //returns True

これを間違った方法で使用していますか?なぜ彼らが私が意図するように比較されないのですか?

編集: 比較器が機能することを示すために、次のようにtrueを返します。

List<SupplierCategory> list1 = new List<SupplierCategory>() {
    new SupplierCategory() { Name = "Cat1" },
    new SupplierCategory() { Name = "Cat2", Parent = "Cat1" },
    new SupplierCategory() { Name = "Cat3", Parent = "Cat2" }
};
List<SupplierCategory> list1 = new List<SupplierCategory>() {
    new SupplierCategory() { Name = "Cat1" },
    new SupplierCategory() { Name = "Cat2", Parent = "Cat1" },
    new SupplierCategory() { Name = "Cat3", Parent = "Cat2" }
};
CategoryPathComparer comp = new CategoryPathComparer();
Console.WriteLine(comp.Equals(list1, list2).ToString());
役に立ちましたか?

解決

問題は、IEqualityComparerを正しく実装しなかったことです。

IEqualityComparer<T> を実装すると、 GetHashCode を実装して、任意の2つのオブジェクトのハッシュコードは同じです。

それ以外の場合、ここに表示されているように、不正な動作が発生します。

次のようにGetHashCodeを実装する必要があります:この回答

public int GetHashCode(List<SupplierCategory> obj) {
    int hash = 17;

    foreach(var value in obj)
        hash = hash * 23 + obj.GetHashCode();

    return hash;
}

一貫性を保つため、SupplierCategoryEqualsもオーバーライドする必要があります。例:

public override int GetHashCode() {
    int hash = 17;
    hash = hash * 23 + Name.GetHashCode();
    hash = hash * 23 + Parent.GetHashCode();
    return hash;
}

最後に、必要はありませんが、おそらくIEquatableの<=>をオーバーライドし、<=>に実装した<=>メソッドを呼び出すようにしてください。

他のヒント

実際には、この問題はドキュメントでも取り上げられています。 http://msdn.microsoft.com/en-us/library/bb338049。 aspx

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