瀏覽代碼

Adding UnionWithAsync to Set.

Bart De Smet 8 年之前
父節點
當前提交
73f6758489

+ 1 - 13
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Distinct.cs

@@ -121,19 +121,7 @@ namespace System.Linq
             {
                 var s = new Set<TSource>(comparer);
 
-                var enu = source.GetAsyncEnumerator();
-
-                try
-                {
-                    while (await enu.MoveNextAsync(cancellationToken).ConfigureAwait(false))
-                    {
-                        s.Add(enu.Current);
-                    }
-                }
-                finally
-                {
-                    await enu.DisposeAsync().ConfigureAwait(false);
-                }
+                await s.UnionWithAsync(source);
 
                 return s;
             }

+ 18 - 0
Ix.NET/Source/System.Linq.Async/System/Linq/Set.cs

@@ -7,6 +7,7 @@
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.Diagnostics.CodeAnalysis;
+using System.Threading.Tasks;
 
 namespace System.Linq
 {
@@ -143,6 +144,23 @@ namespace System.Linq
             _slots = newSlots;
         }
 
+        public async Task UnionWithAsync(IAsyncEnumerable<TElement> other)
+        {
+            var enu = other.GetAsyncEnumerator();
+
+            try
+            {
+                while (await enu.MoveNextAsync().ConfigureAwait(false))
+                {
+                    Add(enu.Current);
+                }
+            }
+            finally
+            {
+                await enu.DisposeAsync().ConfigureAwait(false);
+            }
+        }
+
         internal struct Slot
         {
             internal int _hashCode;