Skip to content

Commit

Permalink
Query: Add AsAsyncEnumerable to public API
Browse files Browse the repository at this point in the history
Resolves #15998

The method cannot take cancellation token because result of WithCancellation is not IAsyncEnumerable
  • Loading branch information
smitpatel committed Jun 10, 2019
1 parent 56aafe4 commit 2795a00
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 59 deletions.
7 changes: 4 additions & 3 deletions src/EFCore/DbSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace Microsoft.EntityFrameworkCore
/// </summary>
/// <typeparam name="TEntity"> The type of entity being operated on by this set. </typeparam>
public abstract class DbSet<TEntity>
: IQueryable<TEntity>, IAsyncEnumerableAccessor<TEntity>, IInfrastructure<IServiceProvider>, IListSource
: IQueryable<TEntity>, IAsyncEnumerable<TEntity>, IInfrastructure<IServiceProvider>, IListSource
where TEntity : class
{
/// <summary>
Expand Down Expand Up @@ -471,11 +471,12 @@ public virtual Task AddRangeAsync(
IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException();

/// <summary>
/// Returns an <see cref="IAsyncEnumerable{T}" /> which when enumerated will asynchronously execute the query against
/// Returns an <see cref="IAsyncEnumerator{T}" /> which when enumerated will asynchronously execute a query against
/// the database.
/// </summary>
/// <returns> The query results. </returns>
IAsyncEnumerable<TEntity> IAsyncEnumerableAccessor<TEntity>.AsyncEnumerable => throw new NotImplementedException();
IAsyncEnumerator<TEntity> IAsyncEnumerable<TEntity>.GetAsyncEnumerator(CancellationToken cancellationToken)
=> throw new NotImplementedException();

/// <summary>
/// Gets the IQueryable element type.
Expand Down
31 changes: 31 additions & 0 deletions src/EFCore/Extensions/EntityFrameworkQueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2903,6 +2903,37 @@ public static async Task ForEachAsync<T>(

#endregion

#region AsAsyncEnumerable

/// <summary>
/// Returns an <see cref="IAsyncEnumerable{T}" /> which can be enumerated asynchronously.
/// </summary>
/// <remarks>
/// Multiple active operations on the same context instance are not supported. Use 'await' to ensure
/// that any asynchronous operations have completed before calling another method on this context.
/// </remarks>
/// <typeparam name="TSource">
/// The type of the elements of <paramref name="source" />.
/// </typeparam>
/// <param name="source">
/// An <see cref="IQueryable{T}" /> to enumerate.
/// </param>
/// <returns> The query results. </returns>
public static IAsyncEnumerable<TSource> AsAsyncEnumerable<TSource>(
[NotNull] this IQueryable<TSource> source)
{
Check.NotNull(source, nameof(source));

if (source is IAsyncEnumerable<TSource> asyncEnumerable)
{
return asyncEnumerable;
}

throw new InvalidOperationException(CoreStrings.IQueryableNotAsync(typeof(TSource)));
}

#endregion

#region Impl.

private static TResult ExecuteAsync<TSource, TResult>(
Expand Down
30 changes: 0 additions & 30 deletions src/EFCore/Extensions/Internal/QueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Query.Internal;
using Microsoft.EntityFrameworkCore.Utilities;

// ReSharper disable CheckNamespace
namespace Microsoft.EntityFrameworkCore.Internal
Expand All @@ -23,30 +17,6 @@ namespace Microsoft.EntityFrameworkCore.Internal
/// </summary>
public static class QueryableExtensions
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static IAsyncEnumerable<TSource> AsAsyncEnumerable<TSource>(
[NotNull] this IQueryable<TSource> source)
{
Check.NotNull(source, nameof(source));

if (source is IAsyncEnumerable<TSource> enumerable)
{
return enumerable;
}

if (source is IAsyncEnumerableAccessor<TSource> entityQueryableAccessor)
{
return entityQueryableAccessor.AsyncEnumerable;
}

throw new InvalidOperationException(CoreStrings.IQueryableNotAsync(typeof(TSource)));
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
Expand Down
5 changes: 3 additions & 2 deletions src/EFCore/Internal/InternalDbSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Microsoft.EntityFrameworkCore.Internal
/// </summary>
public class InternalDbSet<TEntity> :
#pragma warning disable CS0618 // Type or member is obsolete
DbQuery<TEntity>, IQueryable<TEntity>, IAsyncEnumerableAccessor<TEntity>, IInfrastructure<IServiceProvider>, IResettableService
DbQuery<TEntity>, IQueryable<TEntity>, IAsyncEnumerable<TEntity>, IInfrastructure<IServiceProvider>, IResettableService
#pragma warning restore CS0618 // Type or member is obsolete
where TEntity : class
{
Expand Down Expand Up @@ -311,7 +311,8 @@ private IEntityFinder<TEntity> Finder

IEnumerator IEnumerable.GetEnumerator() => EntityQueryable.GetEnumerator();

IAsyncEnumerable<TEntity> IAsyncEnumerableAccessor<TEntity>.AsyncEnumerable => EntityQueryable;
IAsyncEnumerator<TEntity> IAsyncEnumerable<TEntity>.GetAsyncEnumerator(CancellationToken cancellationToken)
=> EntityQueryable.GetAsyncEnumerator(cancellationToken);

Type IQueryable.ElementType => EntityQueryable.ElementType;

Expand Down
24 changes: 0 additions & 24 deletions src/EFCore/Query/Internal/IAsyncEnumerableAccessor.cs

This file was deleted.

0 comments on commit 2795a00

Please sign in to comment.