CSharp examples for System.Collections.Generic:IDictionary
Union two dictionaries with sub-collection of items. If dictionary already contains specified key, it will be replaced by new collection items.
// This program is free software; you can redistribute it and/or modify it using System.Collections.Generic; using System.Collections; public class Main{ /// <summary> /// Union two dictionaries with sub-collection of items. If dictionary already contains specified key, it will be replaced by new collection items. /// </summary> /// <typeparam name="TValue">Dictionary value generic type parameter</typeparam> /// <typeparam name="TKey">Dictionary key generic type parameter</typeparam> /// <param name="target">Target dictionaries</param> /// <param name="source">Source dictionaries</param> public static void UnionDictionaries<TKey, TValue>(IDictionary<TKey, IList<TValue>> target, IDictionary<TKey, IEnumerable<TValue>> source) {//from w w w. ja v a 2 s. co m foreach (KeyValuePair<TKey, IEnumerable<TValue>> item in source) { TKey key = item.Key; if (target.ContainsKey(key)) { target.Remove(key); } // add key and clone source IEnumerable collection target.Add(new KeyValuePair<TKey, IList<TValue>>(item.Key, new List<TValue>(item.Value))); } } /// <summary> /// Union two plain dictionaries. Dictionary items values will be merged (if item with some key from source dictionary is exists in target dictionary, value in target dictionary item will be updated). /// </summary> /// <typeparam name="TValue">Dictionary value generic type parameter</typeparam> /// <typeparam name="TKey">Dictionary key generic type parameter</typeparam> /// <param name="target">Target dictionaries</param> /// <param name="source">Source dictionaries</param> public static void UnionDictionaries<TKey, TValue>(IDictionary<TKey, TValue> target, IDictionary<TKey, TValue> source) { foreach (KeyValuePair<TKey,TValue> item in source) { TKey key = item.Key; if (!target.ContainsKey(key)) { // add value target.Add(item); } else if (Comparer.Default.Compare(target[key], source[key]) != 0) { // update value in target target[key] = source[key]; } } } }