jeudi 6 juillet 2017

c# Comparing two List

I have two Lists of type myObj defined as so:

public class myObj
{
    public int Id {get;set;}
    public string Name {get;set}
    public Address Address {get;set;}
}

This uses another class called Address

public class Address
{
    public int Id {get;set;}
    public string Line1 {get;set}
    public string ZipCode {get;set;}
}

And I have a main sub as below.

sub Main()
{
    var myTestObj1 = new List<myObj>(
        new myObj(){ Id = 1, Name = "Bob", new Address(){ Id = 1, Line 1= "", ZipCode = "90120"}},
        new myObj(){ Id = 2, Name = "Kaz", new Address(){ Id = 2, Line 1= "", ZipCode = "90121"}}
    );
    var myTestObj2 = new List<myObj>(
        new myObj(){ Id = 2, Name = "Kaz", new Address(){ Id = 2, Line 1= "", ZipCode = "00121"}}
        new myObj(){ Id = 3, Name = "Suz", new Address(){ Id = 3, Line 1= "", ZipCode = "99999"}}
    );
}

What I need to do is check the lists for differences. From the above, I am looking for the following.

Looking for the difference between myTestObj1 and myTestObj2

I would expect to see:

myObj id = 1 = New Record
myObj id = 2 = ZipCode has Changed
myObj id = 3 = Record to be deleted

So far I have looked at using reflection, but examples only show one to one comparisons.

C# Compare Two Lists of Different Objects

Difference between two lists

Compare two List objects for equality, ignoring order

If this is a duplicate, can someone point out where to find the solution as I have looked for hours.

Thanks





Aucun commentaire:

Enregistrer un commentaire