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;
|
}
|
}
|
}
|