1
0

AsyncQueryable.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Linq.Expressions;
  6. using System.Reflection;
  7. namespace System.Linq
  8. {
  9. /// <summary>
  10. /// Provides a set of extension methods for asynchronous enumerable sequences represented using expression trees.
  11. /// </summary>
  12. public static partial class AsyncQueryable
  13. {
  14. /// <summary>
  15. /// Converts the specified asynchronous enumerable sequence to an expression representation.
  16. /// </summary>
  17. /// <typeparam name="TElement">The type of the elements in the sequence.</typeparam>
  18. /// <param name="source">The asynchronous enumerable sequence to represent using an expression tree.</param>
  19. /// <returns>An asynchronous enumerable sequence using an expression tree to represent the specified asynchronous enumerable sequence.</returns>
  20. public static IAsyncQueryable<TElement> AsAsyncQueryable<TElement>(this IAsyncEnumerable<TElement> source)
  21. {
  22. if (source == null)
  23. {
  24. throw new ArgumentNullException(nameof(source));
  25. }
  26. if (source is IAsyncQueryable<TElement> queryable)
  27. {
  28. return queryable;
  29. }
  30. return new AsyncEnumerableQuery<TElement>(source);
  31. }
  32. private static MethodInfo? s_Zip__TFirst_TSecond__2__0;
  33. private static MethodInfo Zip__TFirst_TSecond__2__0(Type TFirst, Type TSecond) =>
  34. (s_Zip__TFirst_TSecond__2__0 ??
  35. (s_Zip__TFirst_TSecond__2__0 = new Func<IAsyncQueryable<object>, IAsyncEnumerable<object>, IAsyncQueryable<ValueTuple<object, object>>>(Zip<object, object>).GetMethodInfo()!.GetGenericMethodDefinition())).MakeGenericMethod(TFirst, TSecond);
  36. public static IAsyncQueryable<(TFirst First, TSecond Second)> Zip<TFirst, TSecond>(this IAsyncQueryable<TFirst> first, IAsyncEnumerable<TSecond> second)
  37. {
  38. if (first == null)
  39. throw new ArgumentNullException(nameof(first));
  40. if (second == null)
  41. throw new ArgumentNullException(nameof(second));
  42. return first.Provider.CreateQuery<(TFirst, TSecond)>(Expression.Call(Zip__TFirst_TSecond__2__0(typeof(TFirst), typeof(TSecond)), first.Expression, GetSourceExpression(second)));
  43. }
  44. private static Expression GetSourceExpression<TSource>(IAsyncEnumerable<TSource> source)
  45. {
  46. if (source is IAsyncQueryable<TSource> queryable)
  47. {
  48. return queryable.Expression;
  49. }
  50. return Expression.Constant(source, typeof(IAsyncEnumerable<TSource>));
  51. }
  52. }
  53. }