AsyncQueryable.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. var queryable = source as IAsyncQueryable<TElement>;
  25. if (queryable != null)
  26. {
  27. return queryable;
  28. }
  29. return new AsyncEnumerableQuery<TElement>(source);
  30. }
  31. private static Expression GetSourceExpression<TSource>(IAsyncEnumerable<TSource> source)
  32. {
  33. var queryable = source as IAsyncQueryable<TSource>;
  34. if (queryable != null)
  35. {
  36. return queryable.Expression;
  37. }
  38. return Expression.Constant(source, typeof(IAsyncEnumerable<TSource>));
  39. }
  40. internal static MethodInfo InfoOf<R>(Expression<Func<R>> f)
  41. {
  42. return ((MethodCallExpression)f.Body).Method;
  43. }
  44. }
  45. }