/// <summary> /// Applies the pagination form to the query. /// </summary> /// <param name="query">The query that should be paginated.</param> /// <param name="form">The pagination form that should be applied to the query.</param> /// <returns>A pagination result.</returns> public virtual PaginationResult <TEntity> ApplyPagination(IQueryable <TEntity> query, IPageForm form) { var sorting = form.GetSorting(_parameter)?.ToList(); var alreadySortedByPropertyPaths = new List <string>(); if (sorting != null && sorting.Count > 0) { // first order by user specified sorting bool isSorted = false; foreach (var sort in sorting) { if (sort.MemberAccessor != null) { query = query.OrderBy(_parameter, sort, isSorted); } else { query = ApplyManualOrdering(query, sort.PropertyPath, sort.SortDirection, isSorted); } isSorted = true; alreadySortedByPropertyPaths.Add(sort.PropertyPath); } query = ApplyUniqueSort(alreadySortedByPropertyPaths, (IOrderedQueryable <TEntity>)query); } else { query = ApplyDefaultSort(alreadySortedByPropertyPaths, query); query = ApplyUniqueSort(alreadySortedByPropertyPaths, (IOrderedQueryable <TEntity>)query); } return(CreatePaginationResult(query, form, true)); }
/// <summary> /// Applies the pagination form to the query. /// </summary> /// <param name="query">The query that should be paginated.</param> /// <param name="form">The pagination form that should be applied to the query.</param> /// <returns>A pagination result.</returns> public virtual PaginationResult <TEntity> ApplyPagination(IQueryable <TEntity> query, IPageForm form) { bool isSorted = false; var sorting = form.GetSorting(_parameter)?.ToList(); if (sorting != null && sorting.Count > 0) { // first order by user specified sorting query = query.OrderBy(_parameter, sorting); isSorted = true; if (_uniqueSort != null) { query = ApplyOrdering(query, _uniqueSort, _uniqueSortDirection, ref isSorted); } else if (_defaultSort != null) { query = ApplyOrdering(query, _defaultSort, _defaultSortDirection, ref isSorted); } } else { // first order by default sorting if (_defaultSort != null) { query = ApplyOrdering(query, _defaultSort, _defaultSortDirection, ref isSorted); } // then order by unique sorting, if present, and not equal to unique sorting if (_uniqueSort != null) { if (!(ReferenceEquals(_uniqueSort, _defaultSort) && _uniqueSortDirection == _defaultSortDirection)) { query = ApplyOrdering(query, _uniqueSort, _uniqueSortDirection, ref isSorted); } } } return(CreatePaginationResult(query, form, true)); }
/// <summary> /// Applies the pagination form to the query. /// </summary> /// <param name="query">The query that should be paginated.</param> /// <param name="form">The pagination form that should be applied to the query.</param> /// <returns>A pagination result.</returns> public virtual PaginationResult <TEntity> ApplyPagination(IQueryable <TEntity> query, IPageForm form) { var sorting = form.GetSorting(_parameter)?.ToList(); var alreadySortedByPropertyPaths = new List <string>(); if (sorting != null && sorting.Count > 0) { // first order by user specified sorting query = query.OrderBy(_parameter, sorting); alreadySortedByPropertyPaths.AddRange(sorting.Select(x => x.PropertyPath)); query = ApplyUniqueSort(alreadySortedByPropertyPaths, (IOrderedQueryable <TEntity>)query); } else { query = ApplyDefaultSort(alreadySortedByPropertyPaths, query); query = ApplyUniqueSort(alreadySortedByPropertyPaths, (IOrderedQueryable <TEntity>)query); } return(CreatePaginationResult(query, form, true)); }