9

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:

  1. 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.
  2. 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.

3
  • You could override Equals() in your record object and do the comparison there. Then you could change your where clause to where 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. Commented Aug 29, 2013 at 18:50
  • You might have to list all the columns, maybe I just don't know a better way but I know in TSQL you have to list all columns to compare deltas though there is some groupby tricks for faster comparisons. Commented Aug 29, 2013 at 18:50
  • How do you define a change? I've recently stumbled upon similar problem and I've created a method which combines 2 objects of same type, taking the value from the object of which property is not null or default and it also provides a option to select prioritized object, in case both properties have a value. If that's something you might want, let me know and I'll post a answer with code and better explanation. Commented Aug 29, 2013 at 20:01

2 Answers 2

1

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.

0
  1. Faster but you will need to maintain code if you add new properties is to write custom comparer and use your approach.
  2. Slower use reflection to go through all properties and compare them dynamically

    IDictionary<string, Tuple<object, object>> GetDifferentProperties<T>(T keyValue1, T keyValue2)
    {
       var diff = new Dictionary<string, object>();
       foreach (var prop in typeof(T).GetProperties(BindingFlags.Public))
       {
          var prop1Value = prop.GetValue(keyvalue1);
          var prop2Value = prop.GetValue(keyValue2);
          if (prop1Value != prop2Value)
            diff.Add(prop.Name, Tuple.Create(prop1Value, prop2Value));
       }
       return diff;
    }
    

and then in you code

    var = results = (from u in updated
                    join o in orig
                    select new
                    {
                       Value1 = o.KeyValue,
                       Value2 = u.KeyValue
                    }).ToArray()
                    .Select(x => GetDifferentProperties(x.Value1, x.Value2))
                    .Select(DoSomestufWithDifference);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.