I want to compare two objects with custom type and return the data that has a difference. I override Equals
and GetHashCode
in Address class
and implement a ValueComparer
, however the code below returns all data. Please see expected result below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
public class Person
{
public string FirstName { get; set; }
public string Lastname { get; set; }
public int Id { get; set; }
public List<Address> Address { get; set; }
}
public class Address:IEquatable<Address>
{
public string City { get; set; }
public string Country { get; set; }
public int Id { get; internal set; }
public int PersonId { get; set; }
public override bool Equals(object obj)
{
if (obj == null)
return false;
Address a = obj as Address;
if (object.ReferenceEquals(a, null))
return false;
return (a.City.Equals(City));
}
public virtual bool Equals(Address other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return other.City == City;
}
public override int GetHashCode()
{
return City.GetHashCode();
}
}
public class ValueComparer : IEqualityComparer<Person>
{
public int GetHashCode(Person co)
{
if (co == null)
{
return 0;
}
return co.FirstName.GetHashCode();
}
public bool Equals(Person x1, Person x2)
{
if (object.ReferenceEquals(x1, x2))
{
return true;
}
if (object.ReferenceEquals(x1, null) ||
object.ReferenceEquals(x2, null))
{
return false;
}
return x1.FirstName.Equals(x2.FirstName)
&& x1.Address.Equals(x2.Address);
}
}
class Program
{
static void Main(string[] args)
{
var personsOrig = new List<Person>
{
new Person {Id=1, FirstName = "John", Lastname = "Bill"
, Address=new List<Address>{new Address {Id=1, City = "New York", Country = "US", PersonId=1 }
,new Address {Id=2, City = "Okinawa", Country = "Japan", PersonId=1 }
},
}
,new Person {Id=2, FirstName = "Mary", Lastname = "Doe"
, Address=new List<Address>{new Address {Id=3, City = "Los Angeles", Country = "US", PersonId=2 }
} }
,new Person {Id=3, FirstName = "Joe", Lastname = "McDonalds"
, Address=new List<Address>{new Address {Id=4, City = "California", Country = "US", PersonId=3 }
} }
,new Person {Id=4, FirstName = "Donald", Lastname = "Gates"
, Address=new List<Address>{new Address {Id=5, City = "San Francisco", Country = "US", PersonId=4 }
} }
,new Person {Id=5, FirstName = "Shawn", Lastname = "Porter"
, Address=new List<Address>{new Address {Id=6, City = "Utah", Country = "US", PersonId=5 }
} }
};
var personsNew = new List<Person>
{
new Person {Id=1, FirstName = "John", Lastname = "Bill"
, Address=new List<Address>{
new Address { Id = 1, City = "New York", Country = "US", PersonId = 1 }
}
}
,new Person {Id=2, FirstName = "Mary", Lastname = "Doe"
, Address=new List<Address>{new Address {Id=7, City = "Florida", Country = "US", PersonId=2 }
} }
,new Person {Id=3, FirstName = "Joe", Lastname = "Jean"
, Address=new List<Address>{new Address {Id=4, City = "California", Country = "US", PersonId=3}
} }
,new Person {Id=4, FirstName = "Donald", Lastname = "Gates"
, Address=new List<Address>{new Address {Id=5, City = "San Francisco", Country = "US", PersonId=4 }
} }
,new Person {Id=5, FirstName = "Shawn", Lastname = "Porter"
, Address=new List<Address>{new Address {Id=6, City = "Utah", Country = "US", PersonId=5 }
} }
};
var xxx = personsNew.Except(personsOrig,new ValueComparer()).ToList();
}
}
}
Expected Output
John Bill
Mary Doe
Joe McDonalds