public Dictionary<Y, T> ToDictionarySafe<T, Y>(IEnumerable<T> items, Func<T, Y> keySelector) { var dict = new Dictionary<Y, T>(); foreach (var item in items) { var key = keySelector(item); if (key != null && !dict.ContainsKey(key)) { dict[key] = item; } } return dict; }
294600cookie-checkC# ToDictionary Safe