code_junkie

Merging dictionaries in C#

Nov 12th, 2011
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. public static Dictionary<TKey,TValue>
  2. Merge<TKey,TValue>(Dictionary<TKey,TValue>[] dictionaries);
  3.  
  4. public static Dictionary<TKey,TValue>
  5. Merge<TKey,TValue>(IEnumerable<Dictionary<TKey,TValue>> dictionaries);
  6.  
  7. var result = dictionaries.SelectMany(dict => dict)
  8. .ToDictionary(pair => pair.Key, pair => pair.Value);
  9.  
  10. var result = dictionaries.SelectMany(dict => dict)
  11. .ToLookup(pair => pair.Key, pair => pair.Value)
  12. .ToDictionary(group => group.Key, group => group.First());
  13.  
  14. public static class DictionaryExtensions
  15. {
  16. // Works in C#3/VS2080:
  17. // Returns a new dictionary of this ... others merged leftward.
  18. // Keeps the type of 'this', which must be default-instantiable.
  19. // Example:
  20. // result = map.MergeLeft(other1, other2, ...)
  21. public static T MergeLeft<T,K,V>(this T me, params IDictionary<K,V>[] others)
  22. where T : IDictionary<K,V>, new()
  23. {
  24. T newMap = new T();
  25. foreach (IDictionary<K,V> src in
  26. (new List<IDictionary<K,V>> { me }).Concat(others)) {
  27. // ^-- echk. Not quite there type-system.
  28. foreach (KeyValuePair<K,V> p in src) {
  29. newMap[p.Key] = p.Value;
  30. }
  31. }
  32. return newMap;
  33. }
  34.  
  35. }
  36.  
  37. using System.Collections.Generic;
  38. ...
  39. public static Dictionary<TKey, TValue>
  40. Merge<TKey,TValue>(IEnumerable<Dictionary<TKey, TValue>> dictionaries)
  41. {
  42. var result = new Dictionary<TKey, TValue>();
  43. foreach (var dict in dictionaries)
  44. foreach (var x in dict)
  45. result[x.Key] = x.Value;
  46. return result;
  47. }
  48.  
  49. static Dictionary<TKey, TValue>
  50. Merge<TKey, TValue>(this IEnumerable<Dictionary<TKey, TValue>> enumerable)
  51. {
  52. return enumerable.SelectMany(x => x).ToDictionary(x => x.Key, y => y.Value);
  53. }
  54.  
  55. Dictionary<String, String> allTables = new Dictionary<String, String>();
  56. allTables = tables1.Union(tables2).ToDictionary(pair => pair.Key, pair => pair.Value);
  57.  
  58. public static IDictionary<TKey, TValue> Merge<TKey, TValue>(IEnumerable<IDictionary<TKey, TValue>> dictionaries)
  59. {
  60. // ...
  61. }
  62.  
  63. public static IDictionary<TKey, TValue> Merge<TKey, TValue>(params IDictionary<TKey, TValue>[] dictionaries)
  64. {
  65. return Merge((IEnumerable<TKey, TValue>) dictionaries);
  66. }
  67.  
  68. using System.Collections.Generic;
  69. namespace HelperMethods
  70. {
  71. public static class MergeDictionaries
  72. {
  73. public static void Merge<TKey, TValue>(this IDictionary<TKey, TValue> first, IDictionary<TKey, TValue> second)
  74. {
  75. if (second == null) return;
  76. if (first == null) first = new Dictionary<TKey, TValue>();
  77. foreach (var item in second)
  78. if (!first.ContainsKey(item.Key))
  79. first.Add(item.Key, item.Value);
  80. }
  81. }
  82. }
  83.  
  84. dictionaryFrom.ToList().ForEach(x => dictionaryTo.Add(x.Key, x.Value));
Add Comment
Please, Sign In to add comment