using System; using System.Collections.Generic; using System.Linq; namespace Sony.Filtr.Utility.Extensions { public static class DictionaryExtensions { public static TValue GetValueOrDefault(this IDictionary dictionary, TKey key) { TValue value; return dictionary.TryGetValue(key, out value) ? value : default(TValue); } public static TEnum? GetValueOrNull(this IDictionary dictionary, TKey key) where TEnum : struct, Enum { if (key == null) { return null; } return dictionary.TryGetValue(key, out var value) ? value : (TEnum?)null; } public static TValue GetValueOrDefault(this IDictionary dictionary, TKey key, TValue defaultValue) { TValue value; return dictionary.TryGetValue(key, out value) ? value : defaultValue; } public static Dictionary> ToDictionaryWithListValues(this IEnumerable source, Func getSelector) { return source .GroupBy(getSelector) .ToDictionary(g => g.Key, g => g.ToList()); } } }