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:
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.
You can use the LINQ Except() extension method. That returns everything in a list except what is in the second list.
var orignalContacts = GetOrignal();
var updatedContacts = GetUpdated();
var changedAndNew = updatedContacts.Except(orignalContacts);
var unchanged = orignalContacts.Except(updatedContacts);
Depending on your data provider you may need to overide Equals() and GetHashCode() on your objects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With