using System; using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; namespace Sarand.Net.Standard.Base.Utils { //======================================================================= public static class DictionaryUtil { //------------------------------------------------------------------- // C o n t a i n s K e y s //------------------------------------------------------------------- public static bool ContainsKeys(this Dictionary dict, IEnumerable keys) { bool result = false; keys.ForEachOrBreak((x) => { result = dict.ContainsKey(x); return result; }); return result; } //------------------------------------------------------------------- // G e n e r i c G e t W i t h D e f a u l t //------------------------------------------------------------------- /// Safely get value from Dictionary with default if not defined. public static TValue GetValue(this Dictionary dictionary, TKey key, TValue defaultValue) { return GetValue(dictionary as IDictionary, key, defaultValue); } /// Safely get value from Dictionary with default if not defined. public static TValue GetValue(this IDictionary dictionary, TKey key, TValue defaultValue) { if (key == null) return defaultValue; TValue value; return dictionary.TryGetValue(key, out value) ? value : defaultValue; } //------------------------------------------------------------------- // G e t V a l u e s //------------------------------------------------------------------- public static List GetValues(this Dictionary dict) { return dict.Select(item => item.Value).ToList(); } //------------------------------------------------------------------- // S t r i n g - I n t U t i l i t i e s //------------------------------------------------------------------- /// Increment the count against the specified key by the supplied value, creating the key's entry if needed. public static void Increment(this IDictionary dictionary, String key, int increment = 1) { if (!dictionary.ContainsKey(key)) dictionary[key] = increment; else dictionary[key] += increment; } //------------------------------------------------------------------- // A d d T o L i s t V a l u e //------------------------------------------------------------------- /// Add key:object to dictionary where each value is a list of objects. public static void Add(this Dictionary> dict, TKey key, TValue value) { if (!dict.ContainsKey(key)) dict[key] = new List(); dict[key].Add(value); } /// Add key:objects to dictionary where each value is a list of objects. public static void Add(this Dictionary> dict, TKey key, IEnumerable values) { if (!dict.ContainsKey(key)) dict[key] = new List(); dict[key].AddRange(values); } //------------------------------------------------------------------- // A d d R a n g e //------------------------------------------------------------------- /// Add entries from another dictionary. public static void AddRange(this Dictionary dict, Dictionary dictToAdd) { dictToAdd.ForEach(x => dict[x.Key] = x.Value); } //------------------------------------------------------------------- // Orderd Dictionary //------------------------------------------------------------------- public static DictionaryEntry GetElementAt(this OrderedDictionary dictionary, int index) { return dictionary.Cast().ElementAt(index); } public static K GetKeyAt(this OrderedDictionary dictionary, int index) { return (K)dictionary.GetElementAt(index).Key; } public static V GetValue(this OrderedDictionary dictionary, K key) { if (dictionary == null || key == null) return default(V); try { return (V)dictionary.Cast().AsQueryable().Single(kvp => ((K)kvp.Key).Equals(key)).Value; } catch (Exception) { return default(V); } } //------------------------------------------------------------------- } //======================================================================= }