using System.Linq.Expressions; using LinqToDB; using Nop.Core; namespace System.Linq; public static class AsyncIQueryableExtensions { /// /// Determines whether all the elements of a sequence satisfy a condition /// /// The type of the elements of source /// A sequence whose elements to test for a condition /// A function to test each element for a condition /// /// true if every element of the source sequence passes the test in the specified /// predicate, or if the sequence is empty; otherwise, false /// /// A task that represents the asynchronous operation public static Task AllAsync(this IQueryable source, Expression> predicate) { return AsyncExtensions.AllAsync(source, predicate); } /// /// Determines whether any element of a sequence satisfies a condition /// /// The type of the elements of source /// A sequence whose elements to test for a condition /// A function to test each element for a condition /// /// true if any elements in the source sequence pass the test in the specified predicate; /// otherwise, false /// /// A task that represents the asynchronous operation public static Task AnyAsync(this IQueryable source, Expression> predicate = null) { return predicate == null ? AsyncExtensions.AnyAsync(source) : AsyncExtensions.AnyAsync(source, predicate); } #region Average /// /// Computes the average of a sequence that is obtained by /// invoking a projection function on each element of the input sequence /// /// The type of the elements of source /// A sequence of values to calculate the average of /// A projection function to apply to each element /// /// A task that represents the asynchronous operation /// The task result contains the average of the sequence of values /// public static Task AverageAsync(this IQueryable source, Expression> predicate) { return AsyncExtensions.AverageAsync(source, predicate); } /// /// Computes the average of a sequence that is obtained by /// invoking a projection function on each element of the input sequence /// /// The type of the elements of source /// A sequence of values to calculate the average of /// A projection function to apply to each element /// /// A task that represents the asynchronous operation /// The task result contains the average of the sequence of values /// public static Task AverageAsync(this IQueryable source, Expression> predicate) { return AsyncExtensions.AverageAsync(source, predicate); } /// /// Computes the average of a sequence that is obtained by /// invoking a projection function on each element of the input sequence /// /// The type of the elements of source /// A sequence of values to calculate the average of /// A projection function to apply to each element /// /// A task that represents the asynchronous operation /// The task result contains the average of the sequence of values /// public static Task AverageAsync(this IQueryable source, Expression> predicate) { return AsyncExtensions.AverageAsync(source, predicate); } /// /// Computes the average of a sequence that is obtained by /// invoking a projection function on each element of the input sequence /// /// The type of the elements of source /// A sequence of values to calculate the average of /// A projection function to apply to each element /// /// A task that represents the asynchronous operation /// The task result contains the average of the sequence of values /// public static Task AverageAsync(this IQueryable source, Expression> predicate) { return AsyncExtensions.AverageAsync(source, predicate); } /// /// Computes the average of a sequence that is obtained by /// invoking a projection function on each element of the input sequence /// /// The type of the elements of source /// A sequence of values to calculate the average of /// A projection function to apply to each element /// /// A task that represents the asynchronous operation /// The task result contains the average of the sequence of values /// public static Task AverageAsync(this IQueryable source, Expression> predicate) { return AsyncExtensions.AverageAsync(source, predicate); } /// /// Computes the average of a sequence that is obtained by /// invoking a projection function on each element of the input sequence /// /// The type of the elements of source /// A sequence of values to calculate the average of /// A projection function to apply to each element /// /// A task that represents the asynchronous operation /// The task result contains the average of the sequence of values /// public static Task AverageAsync(this IQueryable source, Expression> predicate) { return AsyncExtensions.AverageAsync(source, predicate); } /// /// Computes the average of a sequence that is obtained by /// invoking a projection function on each element of the input sequence /// /// The type of the elements of source /// A sequence of values to calculate the average of /// A projection function to apply to each element /// /// A task that represents the asynchronous operation /// The task result contains the average of the sequence of values /// public static Task AverageAsync(this IQueryable source, Expression> predicate) { return AsyncExtensions.AverageAsync(source, predicate); } /// /// Computes the average of a sequence that is obtained by /// invoking a projection function on each element of the input sequence /// /// The type of the elements of source /// A sequence of values to calculate the average of /// A projection function to apply to each element /// /// A task that represents the asynchronous operation /// The task result contains the average of the sequence of values /// public static Task AverageAsync(this IQueryable source, Expression> predicate) { return AsyncExtensions.AverageAsync(source, predicate); } /// /// Computes the average of a sequence that is obtained by /// invoking a projection function on each element of the input sequence /// /// The type of the elements of source /// A sequence of values to calculate the average of /// A projection function to apply to each element /// /// A task that represents the asynchronous operation /// The task result contains the average of the sequence of values /// public static Task AverageAsync(this IQueryable source, Expression> predicate) { return AsyncExtensions.AverageAsync(source, predicate); } /// /// Computes the average of a sequence that is obtained by /// invoking a projection function on each element of the input sequence /// /// The type of the elements of source /// A sequence of values to calculate the average of /// A projection function to apply to each element /// /// A task that represents the asynchronous operation /// The task result contains the average of the sequence of values /// public static Task AverageAsync(this IQueryable source, Expression> predicate) { return AsyncExtensions.AverageAsync(source, predicate); } #endregion /// /// Determines whether a sequence contains a specified element by using the default /// equality comparer /// /// The type of the elements of source /// An sequence in which to locate item /// The object to locate in the sequence /// /// true if the input sequence contains an element that has the specified value; /// otherwise, false /// /// A task that represents the asynchronous operation public static Task ContainsAsync(this IQueryable source, TSource item) { return AsyncExtensions.ContainsAsync(source, item); } /// /// Returns the number of elements in the specified sequence that satisfies a condition /// /// The type of the elements of source /// An sequence that contains the elements to be counted /// A function to test each element for a condition /// /// The number of elements in the sequence that satisfies the condition in the predicate /// function /// /// A task that represents the asynchronous operation public static Task CountAsync(this IQueryable source, Expression> predicate = null) { return predicate == null ? AsyncExtensions.CountAsync(source) : AsyncExtensions.CountAsync(source, predicate); } /// /// Returns the first element of a sequence that satisfies a specified condition /// /// /// An sequence to return an element from /// A function to test each element for a condition /// /// A task that represents the asynchronous operation /// The task result contains the first element in source that passes the test in predicate /// public static Task FirstAsync(this IQueryable source, Expression> predicate = null) { return predicate == null ? AsyncExtensions.FirstAsync(source) : AsyncExtensions.FirstAsync(source, predicate); } /// /// Returns the first element of a sequence, or a default value if the sequence contains no elements /// /// The type of the elements of source /// Source /// Predicate /// /// A task that represents the asynchronous operation /// The task result contains the default(TSource) if source is empty; otherwise, the first element in source /// public static Task FirstOrDefaultAsync(this IQueryable source, Expression> predicate = null) { return predicate == null ? AsyncExtensions.FirstOrDefaultAsync(source) : AsyncExtensions.FirstOrDefaultAsync(source, predicate); } /// /// Returns an System.Int64 that represents the number of elements in a sequence /// that satisfy a condition /// /// The type of the elements of source /// An sequence that contains the elements to be counted /// A function to test each element for a condition /// /// The number of elements in source that satisfy the condition in the predicate /// function /// /// A task that represents the asynchronous operation public static Task LongCountAsync(this IQueryable source, Expression> predicate = null) { return predicate == null ? AsyncExtensions.LongCountAsync(source) : AsyncExtensions.LongCountAsync(source, predicate); } /// /// Returns the maximum value in a generic sequence /// /// The type of the elements of source /// A sequence of values to determine the maximum of /// /// A task that represents the asynchronous operation /// The task result contains the maximum value in the sequence /// public static Task MaxAsync(this IQueryable source) { return AsyncExtensions.MaxAsync(source); } /// /// Invokes a projection function on each element of a generic sequence /// and returns the maximum resulting value /// /// The type of the elements of source /// The type of the value returned by the function represented by selector /// A sequence of values to determine the maximum of /// A projection function to apply to each element /// /// A task that represents the asynchronous operation /// The task result contains the maximum value in the sequence /// public static Task MaxAsync(this IQueryable source, Expression> predicate) { return AsyncExtensions.MaxAsync(source, predicate); } /// /// Returns the minimum value in a generic sequence /// /// The type of the elements of source /// A sequence of values to determine the minimum of /// /// A task that represents the asynchronous operation /// The task result contains the minimum value in the sequence /// public static Task MinAsync(this IQueryable source) { return AsyncExtensions.MinAsync(source); } /// /// Invokes a projection function on each element of a generic sequence /// and returns the minimum resulting value /// /// The type of the elements of source /// The type of the value returned by the function represented by selector /// A sequence of values to determine the minimum of /// A projection function to apply to each element /// /// A task that represents the asynchronous operation /// The task result contains the minimum value in the sequence /// public static Task MinAsync(this IQueryable source, Expression> predicate) { return AsyncExtensions.MinAsync(source, predicate); } /// /// Returns the only element of a sequence that satisfies a specified condition, /// and throws an exception if more than one such element exists /// /// /// An sequence to return a single element from /// A function to test an element for a condition /// /// A task that represents the asynchronous operation /// The task result contains the single element of the input sequence that satisfies the condition in predicate /// public static Task SingleAsync(this IQueryable source, Expression> predicate = null) { return predicate == null ? AsyncExtensions.SingleAsync(source) : AsyncExtensions.SingleAsync(source, predicate); } /// /// Returns the only element of a sequence that satisfies a specified condition or /// a default value if no such element exists; this method throws an exception if /// more than one element satisfies the condition /// /// /// A sequence to return a single element from /// A function to test an element for a condition /// /// The single element of the input sequence that satisfies the condition in predicate, /// or default(TSource) if no such element is found /// /// A task that represents the asynchronous operation public static Task SingleOrDefaultAsync(this IQueryable source, Expression> predicate = null) { return predicate == null ? AsyncExtensions.SingleOrDefaultAsync(source) : AsyncExtensions.SingleOrDefaultAsync(source, predicate); } #region Sum /// /// Computes the sum of the sequence that is obtained /// by invoking a projection function on each element of the input sequence /// /// The type of the elements of source /// A sequence of values of type TSource /// A projection function to apply to each element /// /// A task that represents the asynchronous operation /// The task result contains the sum of the projected values /// public static Task SumAsync(this IQueryable source, Expression> predicate) { return AsyncExtensions.SumAsync(source, predicate); } /// /// Computes the sum of the sequence that is obtained /// by invoking a projection function on each element of the input sequence /// /// The type of the elements of source /// A sequence of values of type TSource /// A projection function to apply to each element /// /// A task that represents the asynchronous operation /// The task result contains the sum of the projected values /// public static Task SumAsync(this IQueryable source, Expression> predicate) { return AsyncExtensions.SumAsync(source, predicate); } /// /// Computes the sum of the sequence that is obtained /// by invoking a projection function on each element of the input sequence /// /// The type of the elements of source /// A sequence of values of type TSource /// A projection function to apply to each element /// /// A task that represents the asynchronous operation /// The task result contains the sum of the projected values /// public static Task SumAsync(this IQueryable source, Expression> predicate) { return AsyncExtensions.SumAsync(source, predicate); } /// /// Computes the sum of the sequence that is obtained /// by invoking a projection function on each element of the input sequence /// /// The type of the elements of source /// A sequence of values of type TSource /// A projection function to apply to each element /// /// A task that represents the asynchronous operation /// The task result contains the sum of the projected values /// public static Task SumAsync(this IQueryable source, Expression> predicate) { return AsyncExtensions.SumAsync(source, predicate); } /// /// Computes the sum of the sequence that is obtained /// by invoking a projection function on each element of the input sequence /// /// The type of the elements of source /// A sequence of values of type TSource /// A projection function to apply to each element /// /// A task that represents the asynchronous operation /// The task result contains the sum of the projected values /// public static Task SumAsync(this IQueryable source, Expression> predicate) { return AsyncExtensions.SumAsync(source, predicate); } /// /// Computes the sum of the sequence that is obtained /// by invoking a projection function on each element of the input sequence /// /// The type of the elements of source /// A sequence of values of type TSource /// A projection function to apply to each element /// /// A task that represents the asynchronous operation /// The task result contains the sum of the projected values /// public static Task SumAsync(this IQueryable source, Expression> predicate) { return AsyncExtensions.SumAsync(source, predicate); } /// /// Computes the sum of the sequence that is obtained /// by invoking a projection function on each element of the input sequence /// /// The type of the elements of source /// A sequence of values of type TSource /// A projection function to apply to each element /// /// A task that represents the asynchronous operation /// The task result contains the sum of the projected values /// public static Task SumAsync(this IQueryable source, Expression> predicate) { return AsyncExtensions.SumAsync(source, predicate); } /// /// Computes the sum of the sequence that is obtained /// by invoking a projection function on each element of the input sequence /// /// The type of the elements of source /// A sequence of values of type TSource /// A projection function to apply to each element /// /// A task that represents the asynchronous operation /// The task result contains the sum of the projected values /// public static Task SumAsync(this IQueryable source, Expression> predicate) { return AsyncExtensions.SumAsync(source, predicate); } /// /// Computes the sum of the sequence that is obtained /// by invoking a projection function on each element of the input sequence /// /// The type of the elements of source /// A sequence of values of type TSource /// A projection function to apply to each element /// /// A task that represents the asynchronous operation /// The task result contains the sum of the projected values /// public static Task SumAsync(this IQueryable source, Expression> predicate) { return AsyncExtensions.SumAsync(source, predicate); } /// /// Computes the sum of the sequence that is obtained /// by invoking a projection function on each element of the input sequence /// /// The type of the elements of source /// A sequence of values of type TSource /// A projection function to apply to each element /// /// A task that represents the asynchronous operation /// The task result contains the sum of the projected values /// public static Task SumAsync(this IQueryable source, Expression> predicate) { return AsyncExtensions.SumAsync(source, predicate); } #endregion /// /// Asynchronously loads data from query to a dictionary /// /// Query element type /// Dictionary key type /// Dictionary element type /// Source query /// Source element key selector /// Dictionary element selector /// Dictionary key comparer /// /// A task that represents the asynchronous operation /// The task result contains the dictionary with query results /// public static Task> ToDictionaryAsync( this IQueryable source, Func keySelector, Func elementSelector, IEqualityComparer comparer = null) where TKey : notnull { return comparer == null ? AsyncExtensions.ToDictionaryAsync(source, keySelector, elementSelector) : AsyncExtensions.ToDictionaryAsync(source, keySelector, elementSelector, comparer); } /// /// Asynchronously loads data from query to a dictionary /// /// Query element type /// Dictionary key type /// Source query /// Source element key selector /// Dictionary key comparer /// /// A task that represents the asynchronous operation /// The task result contains the dictionary with query results /// public static Task> ToDictionaryAsync(this IQueryable source, Func keySelector, IEqualityComparer comparer = null) where TKey : notnull { return comparer == null ? AsyncExtensions.ToDictionaryAsync(source, keySelector) : AsyncExtensions.ToDictionaryAsync(source, keySelector, comparer); } /// /// Asynchronously loads data from query to a list /// /// Query element type /// Source query /// /// A task that represents the asynchronous operation /// The task result contains the list with query results /// public static Task> ToListAsync(this IQueryable source) { return AsyncExtensions.ToListAsync(source); } /// /// Asynchronously loads data from query to an array /// /// Query element type /// Source query /// /// A task that represents the asynchronous operation /// The task result contains the array with query results /// public static Task ToArrayAsync(this IQueryable source) { return AsyncExtensions.ToArrayAsync(source); } /// /// Ctor /// /// source /// Page index /// Page size /// A value in indicating whether you want to load only total number of records. Set to "true" if you don't want to load data from database /// A task that represents the asynchronous operation public static async Task> ToPagedListAsync(this IQueryable source, int pageIndex, int pageSize, bool getOnlyTotalCount = false) { if (source == null) return new PagedList(new List(), pageIndex, pageSize); //min allowed page size is 1 pageSize = Math.Max(pageSize, 1); var count = await source.CountAsync(); var data = new List(); if (!getOnlyTotalCount) data.AddRange(await source.Skip(pageIndex * pageSize).Take(pageSize).ToListAsync()); return new PagedList(data, pageIndex, pageSize, count); } }