I have 2 lists, first on parent objects second child objects. Child object has extra property which i want to compare with a property of the parent class.
here is the example
public class Parent
{
public int X { get; set; }
}
public class Child : Parent
{
public int Y { get; set; }
}
public class ClassXCompare : IEqualityComparer<Parent>
{
public bool Equals(Parent x, Parent y)
{
var child = (Child)y;
return x.X == child.Y;
}
public int GetHashCode(Parent parent)
{
int parentXhash = parent.X.GetHashCode();
// Calculate the hash code for the product.
return parentXhash ;
}
}
and now if i test the following, it always fail
var parentList= new List<Parent>
{
new Parent {X = 5},
new Parent {X = 6}
};
var childList= new List<Child>
{
new Child {Y = 5},
new Child {Y = 6}
};
var compare = new ClassXCompare();
var diff = parentList.Except(childList, compare);
Assert.IsTrue(!diff.Any()); // Fail ???
i think my issue is located in the GetHashCode function
Any idea how to solve this?
(Please ignore the design of the application this is simplified version of the issue)