Exemplo n.º 1
0
 /// <summary>
 ///     Asynchronously returns the first element of a sequence, or a default value if the sequence contains no elements.
 /// </summary>
 /// <remarks>
 ///     Multiple active operations on the same context instance are not supported.  Use 'await' to ensure
 ///     that any asynchronous operations have completed before calling another method on this context.
 /// </remarks>
 /// <typeparam name="TSource">
 ///     The type of the elements of <paramref name="source" />.
 /// </typeparam>
 /// <param name="source">
 ///     An <see cref="IQueryable{T}" /> to return the first element of.
 /// </param>
 /// <param name="cancellationToken">
 ///     A <see cref="CancellationToken" /> to observe while waiting for the task to complete.
 /// </param>
 /// <returns>
 ///     A task that represents the asynchronous operation.
 ///     The task result contains <see langword="default" /> ( <typeparamref name="TSource" /> ) if
 ///     <paramref name="source" /> is empty; otherwise, the first element in <paramref name="source" />.
 /// </returns>
 /// <exception cref="ArgumentNullException">
 ///     <paramref name="source" /> is <see langword="null" />.
 /// </exception>
 public static Task <TSource> FirstOrDefaultAsync <TSource>(
     this IYahooQueryable <TSource> source,
     CancellationToken cancellationToken = default
     ) where TSource : IYahooCollection
 {
     return(ExecuteAsync <TSource, TSource>(
                QueryableMethods.FirstOrDefaultWithoutPredicate,
                source,
                cancellationToken
                ));
 }
Exemplo n.º 2
0
        /// <summary>
        ///     Returns an <see cref="IAsyncEnumerable{T}" /> which can be enumerated asynchronously.
        /// </summary>
        /// <remarks>
        ///     Multiple active operations on the same context instance are not supported.  Use 'await' to ensure
        ///     that any asynchronous operations have completed before calling another method on this context.
        /// </remarks>
        /// <typeparam name="TSource">
        ///     The type of the elements of <paramref name="source" />.
        /// </typeparam>
        /// <param name="source">
        ///     An <see cref="IQueryable{T}" /> to enumerate.
        /// </param>
        /// <returns> The query results. </returns>
        /// <exception cref="InvalidOperationException">
        ///     <paramref name="source" /> is <see langword="null" />.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="source" /> is not a <see cref="IAsyncEnumerable{T}" />.
        /// </exception>
        public static IAsyncEnumerable <TSource> AsAsyncEnumerable <TSource>(
            this IYahooQueryable <TSource> source
            ) where TSource : IYahooCollection
        {
            if (source is IAsyncEnumerable <TSource> asyncEnumerable)
            {
                return(asyncEnumerable);
            }

            throw new InvalidOperationException(
                      $"The source 'IQueryable' doesn't implement 'IAsyncEnumerable<{typeof(TSource)}>'. Only sources that implement 'IAsyncEnumerable' can be used for asynchronous operations."
                      );
        }
Exemplo n.º 3
0
 public static IYahooQueryable <TYahooEntity, Game> Filter <TYahooEntity>(
     this IYahooQueryable <TYahooEntity, Game> source,
     GameCollectionFilters filters
     ) where TYahooEntity : IYahooEntity =>
 new YahooCollection <TYahooEntity, Game>(
     source.Provider is YahooQueryProvider
             ? source.Provider.CreateQuery <TYahooEntity>(
         Expression.Call(
             instance : null,
             method : GameFilterMethodInfo.MakeGenericMethod(typeof(TYahooEntity)),
             arguments : new[] { source.Expression, Expression.Constant(filters) }
             )
         )
             : source
     );
Exemplo n.º 4
0
        /// <summary>
        ///     Asynchronously creates a <see cref="List{T}" /> from an <see cref="IQueryable{T}" /> by enumerating it
        ///     asynchronously.
        /// </summary>
        /// <remarks>
        ///     Multiple active operations on the same context instance are not supported.  Use 'await' to ensure
        ///     that any asynchronous operations have completed before calling another method on this context.
        /// </remarks>
        /// <typeparam name="TSource">
        ///     The type of the elements of <paramref name="source" />.
        /// </typeparam>
        /// <param name="source">
        ///     An <see cref="IQueryable{T}" /> to create a list from.
        /// </param>
        /// <param name="cancellationToken">
        ///     A <see cref="CancellationToken" /> to observe while waiting for the task to complete.
        /// </param>
        /// <returns>
        ///     A task that represents the asynchronous operation.
        ///     The task result contains a <see cref="List{T}" /> that contains elements from the input sequence.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="source" /> is <see langword="null" />.
        /// </exception>
        public static async Task <List <TSource> > ToListAsync <TSource>(
            this IYahooQueryable <TSource> source,
            CancellationToken cancellationToken = default
            ) where TSource : IYahooCollection
        {
            var list = new List <TSource>();

            await foreach (
                var element in source.AsAsyncEnumerable().WithCancellation(cancellationToken)
                )
            {
                list.Add(element);
            }

            return(list);
        }
Exemplo n.º 5
0
 public static IYahooQueryable <TSource, TSubCollection> SubResource <
     TSource,
     TPrevSubCollection,
     TSubCollection
     >(
     this IYahooQueryable <TSource, TPrevSubCollection> source,
     Expression <Func <TPrevSubCollection, IEnumerable <TSubCollection> > > expression
     ) where TSource : IYahooEntity
     where TPrevSubCollection : IYahooEntity
     where TSubCollection : IYahooCollection =>
 new YahooCollection <TSource, TSubCollection>(
     source.Provider.CreateQuery <TSource>(
         Expression.Call(
             instance : null,
             method : CollectionNextSubResourceMethodInfo.MakeGenericMethod(
                 typeof(TSource),
                 typeof(TPrevSubCollection),
                 typeof(TSubCollection)
                 ),
             arguments : new[] { source.Expression, Expression.Quote(expression) }
             )
         )
     );
Exemplo n.º 6
0
 /// <summary>
 ///     Asynchronously creates an array from an <see cref="IQueryable{T}" /> by enumerating it asynchronously.
 /// </summary>
 /// <remarks>
 ///     Multiple active operations on the same context instance are not supported.  Use 'await' to ensure
 ///     that any asynchronous operations have completed before calling another method on this context.
 /// </remarks>
 /// <typeparam name="TSource">
 ///     The type of the elements of <paramref name="source" />.
 /// </typeparam>
 /// <param name="source">
 ///     An <see cref="IQueryable{T}" /> to create an array from.
 /// </param>
 /// <param name="cancellationToken">
 ///     A <see cref="CancellationToken" /> to observe while waiting for the task to complete.
 /// </param>
 /// <returns>
 ///     A task that represents the asynchronous operation.
 ///     The task result contains an array that contains elements from the input sequence.
 /// </returns>
 /// <exception cref="ArgumentNullException">
 ///     <paramref name="source" /> is <see langword="null" />.
 /// </exception>
 public static async Task <TSource[]> ToArrayAsync <TSource>(
     this IYahooQueryable <TSource> source,
     CancellationToken cancellationToken = default
     ) where TSource : IYahooCollection =>
 (await source.ToListAsync(cancellationToken).ConfigureAwait(false)).ToArray();