You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IQueryableExtensions.cs 1.2KB

123456789101112131415161718192021222324252627282930313233
  1. using System.Linq.Expressions;
  2. using BlackRock.Reporting.API.Core.Models;
  3. namespace BlackRock.Reporting.API.Extensions
  4. {
  5. public static class IQueryableExtensions
  6. {
  7. public static IQueryable<T> ApplyOrdering<T>(
  8. this IQueryable<T> query,
  9. SortFilter queryObj,
  10. Dictionary<string, Expression<Func<T, object>>> columnsMap)
  11. {
  12. if (string.IsNullOrWhiteSpace(queryObj.SortBy) ||
  13. !columnsMap.ContainsKey(queryObj.SortBy))
  14. return query;
  15. if (queryObj.IsSortAscending.GetValueOrDefault())
  16. return query.OrderBy(columnsMap[queryObj.SortBy]);
  17. return query.OrderByDescending(columnsMap[queryObj.SortBy]);
  18. }
  19. public static IQueryable<T> ApplyPagging<T>(this IQueryable<T> query,PaginationFilter queryObj)
  20. {
  21. if (queryObj.Page is null)
  22. queryObj.Page = 1;
  23. if (queryObj.PageSize is null)
  24. queryObj.PageSize = 10;
  25. return query.Skip((queryObj.Page.GetValueOrDefault() - 1) * queryObj.PageSize.GetValueOrDefault())
  26. .Take(queryObj.PageSize.GetValueOrDefault());
  27. }
  28. }
  29. }