Skip to content

Make EntitySet<T> implement IAsyncEnumerable<T> so that we can use ToListAsync() on EntitySet<T> #395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Orm/Xtensive.Orm.Tests/Linq/EntitySetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using System;
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
using Xtensive.Orm.Tests;
using Xtensive.Orm.Linq;
Expand Down Expand Up @@ -120,6 +121,14 @@ join e in Session.Query.All<Employee>() 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()
{
Expand Down
15 changes: 14 additions & 1 deletion Orm/Xtensive.Orm/Orm/EntitySet{T}.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -59,7 +62,8 @@ namespace Xtensive.Orm
/// <seealso cref="AssociationAttribute.PairTo">Using EntitySets with paired associations</seealso>
public class EntitySet<TItem> : EntitySetBase,
ICollection<TItem>,
IQueryable<TItem>
IQueryable<TItem>,
IAsyncEnumerable<TItem>
where TItem : IEntity
{
private static readonly MemberExpression OwnerPropertyExpression = Expression.Property(Expression.Constant(ownerParameter), ownerParameter.GetType()
Expand Down Expand Up @@ -190,6 +194,15 @@ IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

public async IAsyncEnumerator<TItem> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
var result = await ((QueryProvider)Provider).ExecuteSequenceAsync<TItem>(Expression, cancellationToken).ConfigureAwait(false);
var asyncSource = result.AsAsyncEnumerable().WithCancellation(cancellationToken).ConfigureAwait(false);
await foreach (var element in asyncSource) {
yield return element;
}
}

/// <inheritdoc/>
public void CopyTo(TItem[] array, int arrayIndex)
Expand Down