cloudflight
2024-06-10 45849075b72187ab882e9f5f9d12d5a4b9748f9e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Hydro.CommonBase
{
    public static class ToDictionaryExtentions
    {
        public static ConcurrentDictionary<TKey, TValue> ToDictionaryEx<TElement, TKey, TValue>(
            this IEnumerable<TElement> source,
            Func<TElement, TKey> keyGetter,
            Func<TElement, TValue> valueGetter)
        {
            ConcurrentDictionary<TKey, TValue> dict = new ConcurrentDictionary<TKey, TValue>(); // new Dictionary<TKey, TValue>();
            foreach (var e in source)
            {
                var key = keyGetter(e);
                if (dict.ContainsKey(key))
                {
                    continue;
                }
                dict.TryAdd(key, valueGetter(e));
            }
            return dict;
        }
    }
}