| 123456789101112131415161718192021222324252627282930313233 |
- using System.Linq.Expressions;
- using BlackRock.Reporting.API.Core.Models;
-
- namespace BlackRock.Reporting.API.Extensions
- {
- public static class IQueryableExtensions
- {
- public static IQueryable<T> ApplyOrdering<T>(
- this IQueryable<T> query,
- SortFilter queryObj,
- Dictionary<string, Expression<Func<T, object>>> columnsMap)
- {
- if (string.IsNullOrWhiteSpace(queryObj.SortBy) ||
- !columnsMap.ContainsKey(queryObj.SortBy))
- return query;
- if (queryObj.IsSortAscending.GetValueOrDefault())
- return query.OrderBy(columnsMap[queryObj.SortBy]);
-
- return query.OrderByDescending(columnsMap[queryObj.SortBy]);
- }
- public static IQueryable<T> ApplyPagging<T>(this IQueryable<T> query,PaginationFilter queryObj)
- {
- if (queryObj.Page is null)
- queryObj.Page = 1;
-
- if (queryObj.PageSize is null)
- queryObj.PageSize = 10;
-
- return query.Skip((queryObj.Page.GetValueOrDefault() - 1) * queryObj.PageSize.GetValueOrDefault())
- .Take(queryObj.PageSize.GetValueOrDefault());
- }
- }
- }
|