public static DictDiff Diff(Dictionary <string, string> d1, Dictionary <string, string> d2) { var toSync = new DictDiff(); /* * if (d1 == null) * { * foreach (var kvp in d2) * { * toSync.New.Add(kvp.Key, kvp.Value); * } * } */ foreach (var kvp in d1) { if (d2.ContainsKey(kvp.Key) && kvp.Value != d2[kvp.Key] && d2[kvp.Key].Trim() != "") { toSync.Updated.Add(kvp.Key, d2[kvp.Key]); toSync.Orig.Add(kvp.Key, kvp.Value); } } //don't add new keys foreach (var kvp in d2) { if (!d1.ContainsKey(kvp.Key)) { toSync.New.Add(kvp.Key, kvp.Value); } } return(toSync); }
public virtual void Diff(IEnumerable <T> list1, IEnumerable <T> list2) { var dDiff = new DictDiff(); this.Orig.Clear(); this.Updated.Clear(); this.New.Clear(); this.Deleted.Clear(); var dict1 = list1.ToDictionary(KeySelector); var dict2 = list2.ToDictionary(KeySelector); /* * if (d1 == null) * { * foreach (var kvp in d2) * { * toSync.New.Add(kvp.Key, kvp.Value); * } * } */ foreach (var kvp in dict1) { if (dict2.ContainsKey(kvp.Key) && IsModifiedComparer(ContentSelector(kvp.Value), ContentSelector(dict2[kvp.Key]))) //if (dict2.ContainsKey(kvp.Key) && !ContentSelector(kvp.Value).Equals(ContentSelector(dict2[kvp.Key]))) { Updated.Add(kvp.Key, dict2[kvp.Key]); Orig.Add(kvp.Key, kvp.Value); } } //check new keys foreach (var kvp in dict2) { if (!dict1.ContainsKey(kvp.Key)) { New.Add(kvp.Key, kvp.Value); } } //check deleted keys foreach (var kvp in dict1) { if (!dict2.ContainsKey(kvp.Key)) { Deleted.Add(kvp.Key, kvp.Value); } } }
public static void Patch(Dictionary <string, string> d, DictDiff toSync) { foreach (var kvp in toSync.New) { if (!d.ContainsKey(kvp.Key)) { d.Add(kvp.Key, kvp.Value); } } foreach (var kvp in toSync.Updated) { d[kvp.Key] = kvp.Value; } }