Example #1
0
        private IQueryable <TEntity> PaginatedQueryable(PaginationInfo paginationInfo, SortingInfo sortingInfo, IQueryable <TEntity> queryable)
        {
            paginationInfo.TotalCount = queryable.Count();

            if (!paginationInfo.IsCurrentPageNumberValid())
            {
                return(null);
            }

            if (sortingInfo.Property.ToLowerInvariant() == "id")
            {
                var lambda = Utilities.BuildLambdaForSorting <TEntity, int>(typeof(TEntity), sortingInfo.Property);

                if (lambda.IsSome)
                {
                    queryable = GetOrderedQuery(lambda, queryable, sortingInfo);
                    if (queryable == null)
                    {
                        return(null);
                    }
                }
                else if (CheckSorting(sortingInfo, lambda) == null)
                {
                    return(null);
                }

                var lambda = Utilities.BuildLambdaForSorting <TEntity, int>(typeof(TEntity), sortingInfo.Property).Match(
                    Some: x =>
                {
                    return(x);
                },
                    None: () => { return(null); });
            }
            else
            {
                var lambda = Utilities.BuildLambdaForSorting <TEntity, string>(typeof(TEntity), sortingInfo.Property).Match(
                    Some: x =>
                {
                    return(x);
                },
                    None: () => { return(null); });


                if (CheckSorting(sortingInfo, lambda) == null)
                {
                    return(null);
                }

                queryable = GetOrderedQuery(lambda, queryable, sortingInfo);
                if (queryable == null)
                {
                    return(null);
                }
            }

            return(queryable.Skip(paginationInfo.ItemToSkip).Take(paginationInfo.PageSize));
        }