// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace System.Linq
{
public static partial class AsyncEnumerable
{
///
/// Correlates the elements of two async-enumerable sequences based on equality of keys and groups the results. The default equality comparer is used to compare keys.
///
/// The type of the elements of the first async-enumerable sequence.
/// The type of the elements of the second async-enumerable sequence.
/// The type of the keys returned by the key selector functions.
/// The type of the result elements.
/// The first async-enumerable sequence to join.
/// The async-enumerable sequence to join to the first sequence.
/// A function to extract the join key from each element of the first sequence.
/// A function to extract the join key from each element of the second sequence.
/// A function to create a result element from an element from the first sequence and a collection of matching elements from the second sequence.
/// An async-enumerable sequence that contains elements of type TResult that are obtained by performing a grouped join on two sequences.
/// or or or or is null.
public static IAsyncEnumerable GroupJoin(this IAsyncEnumerable outer, IAsyncEnumerable inner, Func outerKeySelector, Func innerKeySelector, Func, TResult> resultSelector) =>
GroupJoin(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer: null);
///
/// Correlates the elements of two async-enumerable sequences based on equality of keys and groups the results. The specified equality comparer is used to compare keys.
///
/// The type of the elements of the first async-enumerable sequence.
/// The type of the elements of the second async-enumerable sequence.
/// The type of the keys returned by the key selector functions.
/// The type of the result elements.
/// The first async-enumerable sequence to join.
/// The async-enumerable sequence to join to the first async-enumerable sequence.
/// A function to extract the join key from each element of the first sequence.
/// A function to extract the join key from each element of the second sequence.
/// A function to create a result element from an element from the first sequence and a collection of matching elements from the second sequence.
/// An equality comparer to hash and compare keys.
/// An async-enumerable sequence that contains elements of type TResult that are obtained by performing a grouped join on two sequences.
/// or or or or is null.
public static IAsyncEnumerable GroupJoin(this IAsyncEnumerable outer, IAsyncEnumerable inner, Func outerKeySelector, Func innerKeySelector, Func, TResult> resultSelector, IEqualityComparer? comparer)
{
if (outer == null)
throw Error.ArgumentNull(nameof(outer));
if (inner == null)
throw Error.ArgumentNull(nameof(inner));
if (outerKeySelector == null)
throw Error.ArgumentNull(nameof(outerKeySelector));
if (innerKeySelector == null)
throw Error.ArgumentNull(nameof(innerKeySelector));
if (resultSelector == null)
throw Error.ArgumentNull(nameof(resultSelector));
#if HAS_ASYNC_ENUMERABLE_CANCELLATION
return Core();
async IAsyncEnumerable Core([System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
#else
return Create(Core);
async IAsyncEnumerator Core(CancellationToken cancellationToken)
#endif
{
await using var e = outer.GetConfiguredAsyncEnumerator(cancellationToken, false);
if (await e.MoveNextAsync())
{
var lookup = await Internal.Lookup.CreateForJoinAsync(inner, innerKeySelector, comparer, cancellationToken).ConfigureAwait(false);
do
{
var item = e.Current;
var outerKey = outerKeySelector(item);
yield return resultSelector(item, lookup[outerKey].ToAsyncEnumerable());
}
while (await e.MoveNextAsync());
}
}
}
internal static IAsyncEnumerable GroupJoinAwaitCore(this IAsyncEnumerable outer, IAsyncEnumerable inner, Func> outerKeySelector, Func> innerKeySelector, Func, ValueTask> resultSelector) =>
GroupJoinAwaitCore(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer: null);
internal static IAsyncEnumerable GroupJoinAwaitCore(this IAsyncEnumerable outer, IAsyncEnumerable inner, Func> outerKeySelector, Func> innerKeySelector, Func, ValueTask> resultSelector, IEqualityComparer? comparer)
{
if (outer == null)
throw Error.ArgumentNull(nameof(outer));
if (inner == null)
throw Error.ArgumentNull(nameof(inner));
if (outerKeySelector == null)
throw Error.ArgumentNull(nameof(outerKeySelector));
if (innerKeySelector == null)
throw Error.ArgumentNull(nameof(innerKeySelector));
if (resultSelector == null)
throw Error.ArgumentNull(nameof(resultSelector));
#if HAS_ASYNC_ENUMERABLE_CANCELLATION
return Core();
async IAsyncEnumerable Core([System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
#else
return Create(Core);
async IAsyncEnumerator Core(CancellationToken cancellationToken)
#endif
{
await using var e = outer.GetConfiguredAsyncEnumerator(cancellationToken, false);
if (await e.MoveNextAsync())
{
var lookup = await Internal.LookupWithTask.CreateForJoinAsync(inner, innerKeySelector, comparer, cancellationToken).ConfigureAwait(false);
do
{
var item = e.Current;
var outerKey = await outerKeySelector(item).ConfigureAwait(false);
yield return await resultSelector(item, lookup[outerKey].ToAsyncEnumerable()).ConfigureAwait(false);
}
while (await e.MoveNextAsync());
}
}
}
#if !NO_DEEP_CANCELLATION
internal static IAsyncEnumerable GroupJoinAwaitWithCancellationCore(this IAsyncEnumerable outer, IAsyncEnumerable inner, Func> outerKeySelector, Func> innerKeySelector, Func, CancellationToken, ValueTask> resultSelector) =>
GroupJoinAwaitWithCancellationCore(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer: null);
internal static IAsyncEnumerable GroupJoinAwaitWithCancellationCore(this IAsyncEnumerable outer, IAsyncEnumerable inner, Func> outerKeySelector, Func> innerKeySelector, Func, CancellationToken, ValueTask> resultSelector, IEqualityComparer? comparer)
{
if (outer == null)
throw Error.ArgumentNull(nameof(outer));
if (inner == null)
throw Error.ArgumentNull(nameof(inner));
if (outerKeySelector == null)
throw Error.ArgumentNull(nameof(outerKeySelector));
if (innerKeySelector == null)
throw Error.ArgumentNull(nameof(innerKeySelector));
if (resultSelector == null)
throw Error.ArgumentNull(nameof(resultSelector));
#if HAS_ASYNC_ENUMERABLE_CANCELLATION
return Core();
async IAsyncEnumerable Core([System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
#else
return Create(Core);
async IAsyncEnumerator Core(CancellationToken cancellationToken)
#endif
{
await using var e = outer.GetConfiguredAsyncEnumerator(cancellationToken, false);
if (await e.MoveNextAsync())
{
var lookup = await Internal.LookupWithTask.CreateForJoinAsync(inner, innerKeySelector, comparer, cancellationToken).ConfigureAwait(false);
do
{
var item = e.Current;
var outerKey = await outerKeySelector(item, cancellationToken).ConfigureAwait(false);
yield return await resultSelector(item, lookup[outerKey].ToAsyncEnumerable(), cancellationToken).ConfigureAwait(false);
}
while (await e.MoveNextAsync());
}
}
}
#endif
}
}