using System; using System.Collections.Generic; using System.Linq; namespace NzbDrone.Common.Extensions { public static class DictionaryExtensions { public static TValue GetValueOrDefault(this IDictionary dictionary, TKey key, TValue defaultValue = default(TValue)) { TValue value; return dictionary.TryGetValue(key, out value) ? value : defaultValue; } public static Dictionary Merge(this Dictionary first, Dictionary second) { if (first == null) throw new ArgumentNullException("first"); if (second == null) throw new ArgumentNullException("second"); var merged = new Dictionary(); first.ToList().ForEach(kv => merged[kv.Key] = kv.Value); second.ToList().ForEach(kv => merged[kv.Key] = kv.Value); return merged; } public static void Add(this ICollection> collection, TKey key, TValue value) { collection.Add(new KeyValuePair(key, value)); } public static IDictionary SelectDictionary(this IDictionary dictionary, Func, ValueTuple> selection) { return dictionary.Select(selection).ToDictionary(t => t.Item1, t => t.Item2); } public static IDictionary SelectDictionary( this IDictionary dictionary, Func, TNewKey> keySelector, Func, TNewValue> valueSelector) { return dictionary.SelectDictionary(p => { return (keySelector(p), valueSelector(p)); }); } } }