Recently I needed to compare the data from two different databases in two similar but slightly different tables. I needed to sync them so wanted to know which rows to insert, update or delete. So I wrote a generic method to take two collections of keys and compare them using linq.

using System.Collections.Generic;  
using System.Linq;

public class DataSyncComparer<T>  
{
public DataSyncComparer(IEnumerable<T> sourceIds, IEnumerable<T> destination)  
{
// identify inserts, anything in the source and not in the destination
Inserts = sourceIds.Except(destination);        // identify updates, anything which exists in both  
Updates = sourceIds.Intersect(destination);        // identify deletes  
Deletes = destination.Except(sourceIds);  
}

public IEnumerable<T> Inserts { get; private set; }

public IEnumerable<T> Updates { get; private set; }    

public IEnumerable<T> Deletes { get; private set; }  
}

It can be used for simple primary keys as follows

// get our identifiers
List<int> originalIds = new List<int>{1,2,3};  
List<int> existingIds = new List<int>{3,4,5};

// Identify the items to be added, updated, removed
DataSyncComparer<int> sync = new DataSyncComparer<int>(originalIds, existingIds);  

Or for composite keys you can just create a class or structure to hold the correct structure like so

using System;  
using System.Collections.Generic;

public class CompositeKeyTest  
{
   public class MyKey
   {
      public int Key1 { get; set; }
      public string Key2 { get; set; }
   }

   public void TestCompositeKey()
   {
      List<MyKey> source = new List<MyKey>
      {
         new MyKey {1, "Key1"}, 
         new MyKey {2, "Key2"}, 
         new MyKey {3, "Key3"}
      };

      List<MyKey> destination = new List<MyKey>
      {
         new MyKey {3, "Key3"}, 
         new MyKey {4, "Key4"}, 
         new MyKey {5, "Key5"}
      };

      DataSyncComparer<MyKey> comparer = new DataSyncComparer<MyKey>(source, destination);
   }
}