| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- using System;
- using System.Linq;
- using System.Linq.Expressions;
- using Abp.Application.Services.Dto;
- namespace Abp.Linq.Extensions
- {
- /// <summary>
- /// Some useful extension methods for <see cref="IQueryable{T}"/>.
- /// </summary>
- public static class QueryableExtensions
- {
- /// <summary>
- /// Used for paging. Can be used as an alternative to Skip(...).Take(...) chaining.
- /// </summary>
- public static IQueryable<T> PageBy<T>(this IQueryable<T> query, int skipCount, int maxResultCount)
- {
- if (query == null)
- {
- throw new ArgumentNullException("query");
- }
- return query.Skip(skipCount).Take(maxResultCount);
- }
- /// <summary>
- /// Used for paging with an <see cref="IPagedResultRequest"/> object.
- /// </summary>
- /// <param name="query">Queryable to apply paging</param>
- /// <param name="pagedResultRequest">An object implements <see cref="IPagedResultRequest"/> interface</param>
- public static IQueryable<T> PageBy<T>(this IQueryable<T> query, IPagedResultRequest pagedResultRequest)
- {
- return query.PageBy(pagedResultRequest.SkipCount, pagedResultRequest.MaxResultCount);
- }
- /// <summary>
- /// Filters a <see cref="IQueryable{T}"/> by given predicate if given condition is true.
- /// </summary>
- /// <param name="query">Queryable to apply filtering</param>
- /// <param name="condition">A boolean value</param>
- /// <param name="predicate">Predicate to filter the query</param>
- /// <returns>Filtered or not filtered query based on <paramref name="condition"/></returns>
- public static IQueryable<T> WhereIf<T>(this IQueryable<T> query, bool condition, Expression<Func<T, bool>> predicate)
- {
- return condition
- ? query.Where(predicate)
- : query;
- }
- /// <summary>
- /// Filters a <see cref="IQueryable{T}"/> by given predicate if given condition is true.
- /// </summary>
- /// <param name="query">Queryable to apply filtering</param>
- /// <param name="condition">A boolean value</param>
- /// <param name="predicate">Predicate to filter the query</param>
- /// <returns>Filtered or not filtered query based on <paramref name="condition"/></returns>
- public static IQueryable<T> WhereIf<T>(this IQueryable<T> query, bool condition, Expression<Func<T, int, bool>> predicate)
- {
- return condition
- ? query.Where(predicate)
- : query;
- }
- }
- }
|