/// <summary>
        /// 安全的转换成字典集
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TElement"></typeparam>
        /// <param name="source"></param>
        /// <param name="keySelector">键选择器</param>
        /// <param name="elementSelector">值选择器</param>
        /// <returns></returns>
        public static async Task <NullableConcurrentDictionary <TKey, TElement> > ToDictionarySafetyAsync <TSource, TKey, TElement>(this IEnumerable <TSource> source, Func <TSource, TKey> keySelector, Func <TSource, Task <TElement> > elementSelector)
        {
            var dic = new NullableConcurrentDictionary <TKey, TElement>();
            await source.ForeachAsync(async item => dic[keySelector(item)] = await elementSelector(item));

            return(dic);
        }
 /// <summary>
 /// 添加或更新键值对
 /// </summary>
 /// <typeparam name="TKey"></typeparam>
 /// <typeparam name="TValue"></typeparam>
 /// <param name="this"></param>
 /// <param name="that">另一个字典集</param>
 /// <returns></returns>
 public static void AddOrUpdate <TKey, TValue>(this NullableConcurrentDictionary <TKey, TValue> @this, IDictionary <TKey, TValue> that)
 {
     foreach (var item in that)
     {
         @this[item.Key] = item.Value;
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 安全的转换成字典集
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TElement"></typeparam>
        /// <param name="source"></param>
        /// <param name="keySelector">键选择器</param>
        /// <param name="elementSelector">值选择器</param>
        /// <param name="defaultValue">键未找到时的默认值</param>
        /// <returns></returns>
        public static async Task <NullableConcurrentDictionary <TKey, TElement> > ToConcurrentDictionaryAsync <TSource, TKey, TElement>(this IEnumerable <TSource> source, Func <TSource, TKey> keySelector, Func <TSource, Task <TElement> > elementSelector, TElement defaultValue)
        {
            var dic = new NullableConcurrentDictionary <TKey, TElement>()
            {
                FallbackValue = defaultValue
            };
            await source.ForeachAsync(async item => dic[keySelector(item)] = await elementSelector(item));

            return(dic);
        }
        /// <summary>
        /// 安全的转换成字典集
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TElement"></typeparam>
        /// <param name="source"></param>
        /// <param name="keySelector">键选择器</param>
        /// <param name="elementSelector">值选择器</param>
        /// <returns></returns>
        public static NullableConcurrentDictionary <TKey, TElement> ToConcurrentDictionary <TSource, TKey, TElement>(this IEnumerable <TSource> source, Func <TSource, TKey> keySelector, Func <TSource, TElement> elementSelector)
        {
            var dic = new NullableConcurrentDictionary <TKey, TElement>();

            foreach (var item in source)
            {
                dic[keySelector(item)] = elementSelector(item);
            }

            return(dic);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 转换成并发字典集合
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="dic"></param>
        /// <param name="defaultValue">键未找到时的默认值</param>
        /// <returns></returns>
        public static NullableConcurrentDictionary <TKey, TValue> AsConcurrentDictionary <TKey, TValue>(this Dictionary <TKey, TValue> dic, TValue defaultValue)
        {
            var nullableDictionary = new NullableConcurrentDictionary <TKey, TValue>()
            {
                FallbackValue = defaultValue
            };

            foreach (var p in dic)
            {
                nullableDictionary[p.Key] = p.Value;
            }
            return(nullableDictionary);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 安全的转换成字典集
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="source"></param>
        /// <param name="keySelector">键选择器</param>
        /// <param name="defaultValue">键未找到时的默认值</param>
        /// <returns></returns>
        public static NullableConcurrentDictionary <TKey, TSource> ToConcurrentDictionary <TSource, TKey>(this IEnumerable <TSource> source, Func <TSource, TKey> keySelector, TSource defaultValue)
        {
            var dic = new NullableConcurrentDictionary <TKey, TSource>()
            {
                FallbackValue = defaultValue
            };

            foreach (var item in source)
            {
                dic[keySelector(item)] = item;
            }

            return(dic);
        }