Dictionary Delta Extension Method

This method generates a delta dictionary comparing two dictionaries. I typically use this to generate an update SQL statement.

Public Function Delta(Extends data As Dictionary, value As Dictionary) As Dictionary
  // This Method generates a delta dictionary giving the changed/missing keys from value to data.
  Var result As New Dictionary
  For Each entry As DictionaryEntry In data
    If value.HasKey(entry.Key) = False _ ' New Key
      Or value.Value(Entry.Key) <> entry.Value Then ' Changed Value
      result.Value(Entry.Key) = entry.Value ' Add the entry to the Result
    End If
  Next entry
  // Find the keys present in Value but Not in data.  This array will typically be used to write null values to a record in a database.
  Dim nulls() As String
  For Each entry As DictionaryEntry In value
    If Not data.HasKey(entry.Key) Then ' Key is missing from data so add to array
    End If
  Next Entry
  If nulls.Count > 0 Then ' The array will only be added if there are one or more null value fields
    result.Value("Nulls") = nulls ' Add the array to result as table element called Nulls
  End If
  Return result ' A result with no keys shows each dictionary has no delta.
End Function