AsyncQueryable.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 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. throw new ArgumentNullException(nameof(source));
  24. if (source is IAsyncQueryable<TElement> queryable)
  25. {
  26. return queryable;
  27. }
  28. return new AsyncEnumerableQuery<TElement>(source);
  29. }
  30. private static Expression GetSourceExpression<TSource>(IAsyncEnumerable<TSource> source)
  31. {
  32. if (source is IAsyncQueryable<TSource> queryable)
  33. {
  34. return queryable.Expression;
  35. }
  36. return Expression.Constant(source, typeof(IAsyncEnumerable<TSource>));
  37. }
  38. internal static MethodInfo InfoOf<R>(Expression<Func<R>> f)
  39. {
  40. return ((MethodCallExpression)f.Body).Method;
  41. }
  42. }
  43. }