diff --git a/Orm/Xtensive.Orm.Tests/Linq/EntitySetTest.cs b/Orm/Xtensive.Orm.Tests/Linq/EntitySetTest.cs index 99e4a5c39c..3ba6a732c3 100644 --- a/Orm/Xtensive.Orm.Tests/Linq/EntitySetTest.cs +++ b/Orm/Xtensive.Orm.Tests/Linq/EntitySetTest.cs @@ -6,6 +6,7 @@ using System; using System.Linq; +using System.Threading.Tasks; using NUnit.Framework; using Xtensive.Orm.Tests; using Xtensive.Orm.Linq; @@ -120,6 +121,14 @@ join e in Session.Query.All() on i.DesignatedEmployee equals e select e; Assert.AreEqual(customer.Invoices.Count, result.ToList().Count); } + + [Test] + public async Task ToListAsyncTest() + { + var customer = GetCustomer(); + var invoices = await customer.Invoices.ToListAsync(); + Assert.AreEqual(customer.Invoices.Count, invoices.Count); + } private Customer GetCustomer() { diff --git a/Orm/Xtensive.Orm/Orm/EntitySet{T}.cs b/Orm/Xtensive.Orm/Orm/EntitySet{T}.cs index 38b3df05b7..b7f3fd326b 100644 --- a/Orm/Xtensive.Orm/Orm/EntitySet{T}.cs +++ b/Orm/Xtensive.Orm/Orm/EntitySet{T}.cs @@ -1,4 +1,5 @@ // Copyright (C) 2008-2020 Xtensive LLC. +// Copyright (C) 2008-2020 Xtensive LLC. // This code is distributed under MIT license terms. // See the License.txt file in the project root for more information. // Created by: Aleksey Gamzov @@ -11,6 +12,8 @@ using System.Linq; using System.Linq.Expressions; using System.Runtime.Serialization; +using System.Threading; +using System.Threading.Tasks; using Xtensive.Core; using Xtensive.Orm.Internals; using Xtensive.Orm.Linq; @@ -59,7 +62,8 @@ namespace Xtensive.Orm /// Using EntitySets with paired associations public class EntitySet : EntitySetBase, ICollection, - IQueryable + IQueryable, + IAsyncEnumerable where TItem : IEntity { private static readonly MemberExpression OwnerPropertyExpression = Expression.Property(Expression.Constant(ownerParameter), ownerParameter.GetType() @@ -190,6 +194,15 @@ IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } + + public async IAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + var result = await ((QueryProvider)Provider).ExecuteSequenceAsync(Expression, cancellationToken).ConfigureAwait(false); + var asyncSource = result.AsAsyncEnumerable().WithCancellation(cancellationToken).ConfigureAwait(false); + await foreach (var element in asyncSource) { + yield return element; + } + } /// public void CopyTo(TItem[] array, int arrayIndex)