Skip to content
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

Added ToHashSetAsync<T> Extension methods on IQueryable<T> #32905

Merged
merged 1 commit into from
Jan 28, 2024
Merged
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
82 changes: 80 additions & 2 deletions src/EFCore/Extensions/EntityFrameworkQueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2337,6 +2337,84 @@ public static async Task<TSource[]> ToArrayAsync<TSource>(

#endregion

#region ToHashSet

/// <summary>
/// Asynchronously creates a <see cref="HashSet{T}" /> from an <see cref="IQueryable{T}" /> by enumerating it
/// asynchronously.
/// </summary>
/// <remarks>
/// <para>
/// Multiple active operations on the same context instance are not supported. Use <see langword="await" /> to ensure
/// that any asynchronous operations have completed before calling another method on this context.
/// See <see href="https://aka.ms/efcore-docs-threading">Avoiding DbContext threading issues</see> for more information and examples.
/// </para>
/// <para>
/// See <see href="https://aka.ms/efcore-docs-async-linq">Querying data with EF Core</see> for more information and examples.
/// </para>
/// </remarks>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">An <see cref="IQueryable{T}" /> to create a set from.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns>
/// A task that represents the asynchronous operation.
/// The task result contains a <see cref="HashSet{T}" /> that contains elements from the input sequence.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
/// <exception cref="OperationCanceledException">If the <see cref="CancellationToken" /> is canceled.</exception>
public static async Task<HashSet<TSource>> ToHashSetAsync<TSource>(
this IQueryable<TSource> source,
CancellationToken cancellationToken = default)
{
var set = new HashSet<TSource>();
await foreach (var element in source.AsAsyncEnumerable().WithCancellation(cancellationToken).ConfigureAwait(false))
{
set.Add(element);
}

return set;
}

/// <summary>
/// Asynchronously creates a <see cref="HashSet{T}" /> from an <see cref="IQueryable{T}" /> by enumerating it
/// asynchronously.
/// </summary>
/// <remarks>
/// <para>
/// Multiple active operations on the same context instance are not supported. Use <see langword="await" /> to ensure
/// that any asynchronous operations have completed before calling another method on this context.
/// See <see href="https://aka.ms/efcore-docs-threading">Avoiding DbContext threading issues</see> for more information and examples.
/// </para>
/// <para>
/// See <see href="https://aka.ms/efcore-docs-async-linq">Querying data with EF Core</see> for more information and examples.
/// </para>
/// </remarks>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">An <see cref="IQueryable{T}" /> to create a set from.</param>
/// <param name="comparer">The <see cref="IEqualityComparer{T}"/> implementation to use when comparing values in the set, or null to use the default <see cref="EqualityComparer{T}"/> implementation for the set type.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns>
/// A task that represents the asynchronous operation.
/// The task result contains a <see cref="HashSet{T}" /> that contains elements from the input sequence.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
/// <exception cref="OperationCanceledException">If the <see cref="CancellationToken" /> is canceled.</exception>
public static async Task<HashSet<TSource>> ToHashSetAsync<TSource>(
this IQueryable<TSource> source,
IEqualityComparer<TSource>? comparer,
CancellationToken cancellationToken = default)
{
var set = new HashSet<TSource>(comparer);
await foreach (var element in source.AsAsyncEnumerable().WithCancellation(cancellationToken).ConfigureAwait(false))
{
set.Add(element);
}

return set;
}

#endregion

#region Include

internal static readonly MethodInfo IncludeMethodInfo
Expand Down Expand Up @@ -2834,8 +2912,8 @@ source.Provider is EntityQueryProvider
/// </exception>
public static IQueryable<T> TagWithCallSite<T>(
this IQueryable<T> source,
[NotParameterized] [CallerFilePath] string? filePath = null,
[NotParameterized] [CallerLineNumber] int lineNumber = 0)
[NotParameterized][CallerFilePath] string? filePath = null,
[NotParameterized][CallerLineNumber] int lineNumber = 0)
=> source.Provider is EntityQueryProvider
? source.Provider.CreateQuery<T>(
Expression.Call(
Expand Down
4 changes: 4 additions & 0 deletions test/EFCore.Tests/Extensions/QueryableExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ await SourceNonAsyncEnumerableTest<int>(
() => Source().ToDictionaryAsync(e => e, e => e, ReferenceEqualityComparer.Instance));
await SourceNonAsyncEnumerableTest<int>(
() => Source().ToDictionaryAsync(e => e, e => e, ReferenceEqualityComparer.Instance, new CancellationToken()));
await SourceNonAsyncEnumerableTest<int>(() => Source().ToHashSetAsync());
await SourceNonAsyncEnumerableTest<int>(() => Source().ToHashSetAsync(EqualityComparer<int>.Default));
await SourceNonAsyncEnumerableTest<int>(
() => Source().ToHashSetAsync(EqualityComparer<int>.Default, new CancellationToken()));
await SourceNonAsyncEnumerableTest<int>(() => Source().ToListAsync());

Assert.Equal(
Expand Down
Loading