using System; using System.Collections.Generic; namespace zero.Core.Extensions { public static class DictionaryExtensions { public static bool TryGetValue(this Dictionary model, string key, out T value) { if (!model.TryGetValue(key, out object valueObj) || !(valueObj is T)) { value = default; return false; } value = (T)valueObj; return true; } public static T GetValueOrDefault(this Dictionary model, string key) { object value = model.GetValueOrDefault(key); return value == default || !(value is T) ? default : (T)value; } public static Dictionary ToDistinctDictionary(this IEnumerable source, Func keySelector, Func elementSelector) { Dictionary result = new(); foreach (TSource sourceElement in source) { result.TryAdd(keySelector(sourceElement), elementSelector(sourceElement)); } return result; } } }