AsyncQueryable.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. using System.Collections.Generic;
  3. using System.Linq.Expressions;
  4. using System.Reflection;
  5. namespace System.Linq
  6. {
  7. /// <summary>
  8. /// Provides a set of extension methods for asynchronous enumerable sequences represented using expression trees.
  9. /// </summary>
  10. public static partial class AsyncQueryable
  11. {
  12. /// <summary>
  13. /// Converts the specified asynchronous enumerable sequence to an expression representation.
  14. /// </summary>
  15. /// <typeparam name="TElement">The type of the elements in the sequence.</typeparam>
  16. /// <param name="source">The asynchronous enumerable sequence to represent using an expression tree.</param>
  17. /// <returns>An asynchronous enumerable sequence using an expression tree to represent the specified asynchronous enumerable sequence.</returns>
  18. public static IAsyncQueryable<TElement> AsAsyncQueryable<TElement>(this IAsyncEnumerable<TElement> source)
  19. {
  20. if (source == null)
  21. throw new ArgumentNullException("source");
  22. var queryable = source as IAsyncQueryable<TElement>;
  23. if (queryable != null)
  24. {
  25. return queryable;
  26. }
  27. return new AsyncEnumerableQuery<TElement>(source);
  28. }
  29. private static Expression GetSourceExpression<TSource>(IAsyncEnumerable<TSource> source)
  30. {
  31. var queryable = source as IAsyncQueryable<TSource>;
  32. if (queryable != null)
  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. }