浏览代码

增加LookupX函数

懒得勤快 1 年之前
父节点
当前提交
946c207a92

+ 86 - 2
Masuit.Tools.Abstractions/Extensions/BaseType/IDictionaryExtensions.cs

@@ -103,7 +103,7 @@ public static class IDictionaryExtensions
 
         return @this[key];
     }
-    
+
     /// <summary>
     /// 添加或更新键值对
     /// </summary>
@@ -1103,6 +1103,90 @@ public static class IDictionaryExtensions
         return dic;
     }
 
+    /// <summary>
+    /// 转换为Lookup
+    /// </summary>
+    /// <typeparam name="TSource"></typeparam>
+    /// <typeparam name="TKey"></typeparam>
+    /// <param name="source"></param>
+    /// <param name="keySelector">键选择器</param>
+    public static Dictionary<TKey, List<TSource>> ToLookupX<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
+    {
+        var items = source as IList<TSource> ?? source.ToList();
+        var dic = new Dictionary<TKey, List<TSource>>(items.Count);
+        foreach (var item in items)
+        {
+            var key = keySelector(item);
+            if (dic.TryGetValue(key, out var list))
+            {
+                list.Add(item);
+            }
+            else
+            {
+                dic.Add(key, new List<TSource> { item });
+            }
+        }
+
+        return dic;
+    }
+
+    /// <summary>
+    /// 转换为Lookup
+    /// </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>
+    public static Dictionary<TKey, List<TElement>> ToLookupX<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
+    {
+        var items = source as IList<TSource> ?? source.ToList();
+        var dic = new Dictionary<TKey, List<TElement>>(items.Count);
+        foreach (var item in items)
+        {
+            var key = keySelector(item);
+            if (dic.TryGetValue(key, out var list))
+            {
+                list.Add(elementSelector(item));
+            }
+            else
+            {
+                dic.Add(key, new List<TElement> { elementSelector(item) });
+            }
+        }
+
+        return dic;
+    }
+
+    /// <summary>
+    /// 转换为Lookup
+    /// </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>
+    public static async Task<ConcurrentDictionary<TKey, List<TElement>>> ToLookupAsync<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, Task<TElement>> elementSelector)
+    {
+        var items = source as IList<TSource> ?? source.ToList();
+        var dic = new ConcurrentDictionary<TKey, List<TElement>>();
+        await items.ForeachAsync(async item =>
+        {
+            var key = keySelector(item);
+            if (dic.TryGetValue(key, out var list))
+            {
+                list.Add(await elementSelector(item));
+            }
+            else
+            {
+                dic.TryAdd(key, new List<TElement> { await elementSelector(item) });
+            }
+        });
+        return dic;
+    }
+
     /// <summary>
     /// 转换成并发字典集合
     /// </summary>
@@ -1150,4 +1234,4 @@ public static class IDictionaryExtensions
 
         return nullableDictionary;
     }
-}
+}

+ 1 - 1
Masuit.Tools.sln

@@ -29,7 +29,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masuit.Tools.AspNetCore", "
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCoreTest", "NetCoreTest\NetCoreTest.csproj", "{144D7A0C-002D-48E4-8814-EA14011CFFFC}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BenchmarkTest", "BenchmarkTest\BenchmarkTest.csproj", "{0599ACF0-8495-4E72-AB5E-B7446A5C413A}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BenchmarkTest", "BenchmarkTest\BenchmarkTest.csproj", "{0599ACF0-8495-4E72-AB5E-B7446A5C413A}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution

+ 1 - 1
Masuit.Tools/Masuit.Tools.csproj

@@ -224,7 +224,7 @@
       <Version>1.0.0</Version>
     </PackageReference>
     <PackageReference Include="StackExchange.Redis">
-      <Version>2.7.33</Version>
+      <Version>2.8.0</Version>
     </PackageReference>
     <PackageReference Include="System.Text.Json">
       <Version>8.0.3</Version>