I have two objects with the same properties. What I need is the list of properties having a difference in values. Consider the following classes (there can be many properties but they will always be the same in both the classes). In the example, I have two objects with a difference in lastname
and address
. So, I need a list that contains the property name along with the old and new values.
public class A
{
public string ColumnName { get; set; }
public string oldValue { get; set; }
public string newValue { get; set; }
}
public class B
{
public string ColumnName { get; set; }
public string oldValue { get; set; }
public string newValue { get; set; }
}
public class C
{
public string ColumnName { get; set; }
public string oldValue { get; set; }
public string newValue { get; set; }
}
A objA = new A();
objA.firstName = "XYZ";
objA.lastName = "ABC";
objA.address = "123";
B objB = new B();
objB.firstName = "XYZ";
objB.lastName = "ABCD";
objB.address = "456";
List<C> listObj = new List<C>();
//Here I need a list of the columns having different value. For example this should be the result after comparison.
listObj.Add(new C{ColumnName = "lastName", oldValue = "ABC", newValue = "ABCD"});
listObj.Add(new C{ColumnName = "address", oldValue = "123", newValue = "456"});
Is there a way this can be achieved (preferably LINQ but not necessary)?
EDIT: For those looking for a solution, this is exactly what I was looking for: CompareNETObjects