static public HashSet <T> ToHashSet <T>(this IEnumerable <T> enumerable, ConservedKey conservedKey) { var hashSet = new HashSet <T>(); foreach (T obj in enumerable) { switch (conservedKey) { case ConservedKey.FirstOccurence: hashSet.Add(obj); break; case ConservedKey.LastOccurence: hashSet.Remove(obj); hashSet.Add(obj); break; default: throw new NotSupportedException(); } } return(hashSet); }
static public Dictionary <T1, T2> ToDictionary <T1, T2>(this IEnumerable <Tuple <T1, T2> > enumerable, ConservedKey conservedKey) { return(enumerable.ToDictionary(x => x.Item1, x => x.Item2, conservedKey)); }
static public Dictionary <TKey, TValue> ToDictionary <T, TKey, TValue>(this IEnumerable <T> enumerable, Func <T, TKey> keySelector, Func <T, TValue> valueSelector, ConservedKey conservedKey) { var dictionary = new Dictionary <TKey, TValue>(); foreach (T obj in enumerable) { TKey key = keySelector(obj); switch (conservedKey) { case ConservedKey.FirstOccurence: if (dictionary.ContainsKey(key)) { dictionary.Add(key, valueSelector(obj)); } break; case ConservedKey.LastOccurence: dictionary[key] = valueSelector(obj); break; default: throw new NotSupportedException(); } } return(dictionary); }
static public Dictionary <TKey, TValue> ToDictionary <TKey, TValue>(this IEnumerable <KeyValuePair <TKey, TValue> > enumerable, ConservedKey conservedKey) { return(enumerable.ToDictionary(x => x.Key, x => x.Value, conservedKey)); }