Exemple #1
0
        public virtual PaginationVM <MT> GetPaginated <MT>(int page, int pageSize, IList <MT> data = null, bool orderByUser = false, bool?getDeletedRegisters = false) where MT : BaseEntity
        {
            if (page < 1)
            {
                return(PaginationVM <MT> .Empty());
            }

            IQueryable <MT> result = data.AsQueryable();

            int totalPages   = (int)Math.Ceiling((decimal)result.Count() / pageSize);
            int totalRecords = result.Count();

            if (orderByUser)
            {
                result = result.Skip((page - 1) * pageSize).Take(pageSize);
            }
            else
            {
                result = result.OrderByDescending(x => x.GetType().GetProperty("Id").GetValue(x)).Skip((page - 1) * pageSize).Take(pageSize);
            }

            return(new PaginationVM <MT> {
                Data = result.ToList(), TotalPages = totalPages, TotalData = totalRecords
            });
        }
Exemple #2
0
        public virtual PaginationVM <MT> GetPaginated <MT>(int page, int pageSize, Expression <Func <TEntity, bool> > where = null, IList <string> includes = null, Expression <Func <TEntity, object> > orderBy = null, TypeOrderBy tipoOrderBy = TypeOrderBy.Ascending, Expression <Func <TEntity, object> > thenBy = null, bool?getDeletedRegisters = false) where MT : BaseEntity
        {
            if (page < 1)
            {
                return(PaginationVM <MT> .Empty());
            }

            IQueryable <TEntity> result = GetAll(includes, getDeletedRegisters).Result;

            if (where != null)
            {
                result = result.Where(where);
            }

            if (orderBy != null)
            {
                if (tipoOrderBy == TypeOrderBy.Ascending)
                {
                    if (thenBy != null)
                    {
                        result = result.OrderBy(orderBy).ThenBy(thenBy);
                    }
                    else
                    {
                        result = result.OrderBy(orderBy);
                    }
                }
                else
                {
                    if (thenBy != null)
                    {
                        result = result.OrderByDescending(orderBy).ThenByDescending(thenBy);
                    }
                    else
                    {
                        result = result.OrderByDescending(orderBy);
                    }
                }
            }

            int totalPages   = (int)Math.Ceiling((decimal)result.Count() / pageSize);
            int totalRecords = result.Count();

            result = result.Skip((page - 1) * pageSize).Take(pageSize);

            List <MT> ListaMt = orderBy != null?result.Select(mapper.Map <TEntity, MT>).ToList() : result.Select(mapper.Map <TEntity, MT>).OrderByDescending(x => x.GetType().GetProperty("Id").GetValue(x)).ToList();

            return(new PaginationVM <MT> {
                Data = ListaMt, TotalPages = totalPages, TotalData = totalRecords
            });
        }