I need to compare two lists, that contains objects of the same class, however not every property should be used to check for difference. Eg. the Id (primary key) property should not be used.
My example class:
class MyClass
{
public System.Nullable<int> Id { get; set; }
public int VMId { get; set; }
public string Name { get; set; }
}
var oldR = new List<MyClass>();
oldR.Add(new MyClass() { Id = 1, VMId = 11, Name = "Test1" });
oldR.Add(new MyClass() { Id = 2, VMId = 22, Name = "Test2" });
oldR.Add(new MyClass() { Id = 3, VMId = 33, Name = "Test3" });
oldR.Add(new MyClass() { Id = 4, VMId = 44, Name = "Test4" });
oldR.Add(new MyClass() { Id = 5, VMId = 55, Name = "Test5" });
var newR = new List<MyClass>();
newR.Add(new MyClass() { Id = null, VMId = 22, Name = "Test2" });
newR.Add(new MyClass() { Id = null, VMId = 33, Name = "Test3" });
newR.Add(new MyClass() { Id = null, VMId = 44, Name = "Test43" });
newR.Add(new MyClass() { Id = null, VMId = 55, Name = "Test5" });
newR.Add(new MyClass() { Id = null, VMId = 66, Name = "Test6" });
What I would like to do with the above, is to match the two lists on the VMId property. Next I would like to check if the Name property has changed from oldR to newR.
Basically I would like to now, that Test1 should be removed from database, Test6 should be added, and Test4 should gets updated to Test43.
Hope it makes sence
EDIT: I should mention that my real class has +20 properties. I would like to now how to use linq to compare, without specifying them all? And maybe just exclude the Id property that I don't want to compare
How can I accomplish that?