I'm trying to figure out how best to compare and merge two List<T>
with a new List<T>
being generated that compares multiple properties within each object.
class Account
{
public Account() { }
public string ID { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
List<Account> Mine = new List<Account>();
List<Account> Yours = new List<Account>();
List<Account> Ours = new List<Account>();
Account m1 = new Account(){ ID = null, Name = "C_First", Value = "joe" };
Account m2 = new Account(){ ID = null, Name = "C_Last", Value = "bloggs" };
Account m3 = new Account(){ ID = null, Name = "C_Car", Value = "ford" };
Mine.Add(m1);
Mine.Add(m2);
Mine.Add(m3);
Account y1 = new Account(){ ID = "1", Name = "C_First", Value = "john" };
Account y2 = new Account(){ ID = "2", Name = "C_Last", Value = "public" };
Yours.Add(y1);
Yours.Add(y2);
The resulting List<Account> Ours
would have the following List<Account>
objects:
{ ID = "1", Name = "C_First", Value = "joe" };
{ ID = "2", Name = "C_Last", Value = "bloggs" };
{ ID = null, Name = "C_Car", Value = "ford" };
I need to figure out how best to compare the ID and Value properties between both List<Account>
objects where the List<Account> Yours
ID takes precedence over the List<Account> Mine
and the List<Account> Mine
Value takes precedence over List<Account> Yours
along with any object that's not in List<Account> Yours
being added as well.
I've tried the following:
Ours = Mine.Except(Yours).ToList();
which results in List<Ours>
being empty.
I've read this post Difference between two lists in which Jon Skeet mentions using a custom IEqualityComparer<T>
to do what I need but I'm stuck on how to create an IEqualityComparer<T>
that compares more than 1 property value.