I've scoured the internet to try and find a solution for this, and perhaps I'm not going about this the right way.
I need to compare two datasets, identical in structure, and would like to find new and changed objects (using LINQ).
Using what I've found at CodeProject, I was able to pull together a list of items that have changed, but this was done by hard-coding each column (and there will be many) and checking for identical values:
var updRec = from u in updated
join o in orig
on u.KeyValue equals o.KeyValue
where
(o.Propery1 != u.Propery1) ||
(o.Propery2 != u.Propery2)
select new record
{
KeyValue = u.KeyValue,
Propery1 = u.Propery1,
Propery2 = u.Propery2 ,
RecordType = "mod" // Modified
};
I could use help with 2 things:
- Is there a more efficient way to loop through each of the columns, as I plan on adding many more properties that I need to compare? There has to be a better way to dynamically check 2 identical datasets for changes.
- How can I see which propery has changed? For example, creating a list of 'Property, OriginalValue, UpdatedValue' for all items that are not identical?
Hopefully that explains it well. Please feel free to point me at other ways of handling this scenario if I'm not looking at it correctly.
Equals()
in yourrecord
object and do the comparison there. Then you could change yourwhere
clause towhere o.Equals(u)
. But that's just moving the comparison to a more encapsulated place. If you want more automation you will probably need to use reflection.