1
1
懒得勤快 9 сар өмнө
parent
commit
c1743d8215

+ 32 - 3
Masuit.Tools.Abstractions/Systems/LookupX.cs

@@ -5,7 +5,7 @@ using System.Linq;
 
 namespace Masuit.Tools.Systems;
 
-public class LookupX<TKey, TElement> : IEnumerable<KeyValuePair<TKey, List<TElement>>>
+public class LookupX<TKey, TElement> : IEnumerable<Grouping<TKey, TElement>>
 {
     private readonly IDictionary<TKey, List<TElement>> _dictionary;
 
@@ -19,9 +19,9 @@ public class LookupX<TKey, TElement> : IEnumerable<KeyValuePair<TKey, List<TElem
         _dictionary = dic;
     }
 
-    public IEnumerator<KeyValuePair<TKey, List<TElement>>> GetEnumerator()
+    public IEnumerator<Grouping<TKey, TElement>> GetEnumerator()
     {
-        return _dictionary.Select(pair => pair).GetEnumerator();
+        return _dictionary.Select(pair => new Grouping<TKey, TElement>(pair.Key, pair.Value)).GetEnumerator();
     }
 
     IEnumerator IEnumerable.GetEnumerator()
@@ -38,3 +38,32 @@ public class LookupX<TKey, TElement> : IEnumerable<KeyValuePair<TKey, List<TElem
 
     public List<TElement> this[TKey key] => _dictionary.TryGetValue(key, out var value) ? value : new List<TElement>();
 }
+
+public class Grouping<TKey, TElement> : IEnumerable<TElement>
+{
+    private readonly List<TElement> _list;
+
+    internal Grouping(TKey key, List<TElement> list)
+    {
+        Key = key;
+        _list = list;
+    }
+
+    public IEnumerator<TElement> GetEnumerator()
+    {
+        return _list.GetEnumerator();
+    }
+
+    IEnumerator IEnumerable.GetEnumerator()
+    {
+        return GetEnumerator();
+    }
+
+    public TKey Key { get; }
+
+    public void Deconstruct(out TKey key, out IEnumerable<TElement> elements)
+    {
+        key = Key;
+        elements = _list;
+    }
+}