同じクラスの 2 つのオブジェクトがあります。名前が であるとしましょう。これらClass1
の2 つのオブジェクトが(string one)の 1 つのプロパティに基づいて
まったく同じ ( values と count ) であることを示す最良の方法は何ですか?Class1
EntitySet
ClassChild
ClassChild's
EntitySets
ClassChild
ありがとうございました。
SequenceEqual
-methodを使用できます。
bool equal = obj1.ClassChildren.SequenceEqual(obj2.ClassChildren)
これは、デフォルトの等式比較器を使用してカスタムのものを使用します。ここまたはこの例を参照してください。
class ClassChildComparer : IEqualityComparer<ClassChild>
{
public bool Equals(ClassChild x, ClassChild y)
{
return x.Property == y.Property;
}
// If Equals() returns true for a pair of objects then GetHashCode() must return the same value for these objects.
public int GetHashCode(ClassChild c)
{
return c.Property.GetHashCode();
}
}
//and then:
bool equal = obj1.ClassChildren.SequenceEqual(obj2.ClassChildren, new ClassChildComparer())