0

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

gilesrpa
  • 969
  • 1
  • 12
  • 35
  • Find `LINQ.Except` on internet with `EqualityComparer`. – Nikhil Agrawal Jul 06 '17 at 14:37
  • You're most likely going to end up comparing the individual objects inside the lists at some point and there's a solid answer for that here - https://stackoverflow.com/questions/2387946/finding-property-differences-between-two-c-sharp-objects – Jordan Jul 06 '17 at 15:06

1 Answers1

0

Well thanks for the advice, but in the end I have chosen a simpler way of doing this.

I have added a new property to the viewmodel that holds an enum value for either add, delete, update, or no change. The view has this field added as a hidden input. Then it is updated as and when by some java script.

gilesrpa
  • 969
  • 1
  • 12
  • 35