Ejemplo n.º 1
0
        public Tuple <PagingReturnModel, IEnumerable <UserModel> > Page(Func <UserProfile, bool> whereClause, Func <UserProfile, IComparable> orderClause, bool isAsc, int pageNumber, int pageSize)
        {
            int CurrentPage = pageNumber;
            int PageSize    = pageSize;
            int TotalCount  = 0;
            int TotalPages  = (int)Math.Ceiling(TotalCount / (double)PageSize);
            IEnumerable <UserModel> Data;
            var roleData = Context.Roles.ToList();

            if (orderClause == null)
            {
                var data = Context.Set <UserProfile>().Where(whereClause);
                TotalCount = data.Count();
                Data       = data.Skip((CurrentPage - 1) * PageSize).Take(PageSize).ToList().Select(x => new UserModel {
                    FirstName = x.FirstName, Id = x.Id, LastName = x.LastName, UserName = x.User.UserName, Role = roleData.First(y => y.Id == x.User.Roles.FirstOrDefault().RoleId).Name, RoleId = x.User.Roles.FirstOrDefault().RoleId
                });
            }
            else
            {
                if (isAsc)
                {
                    var data = Context.Set <UserProfile>().Where(whereClause);
                    TotalCount = data.Count();
                    Data       = data.OrderBy(orderClause).Skip((CurrentPage - 1) * PageSize).Take(PageSize).ToList().Select(x => new UserModel {
                        FirstName = x.FirstName, Id = x.Id, LastName = x.LastName, UserName = x.User.UserName, Role = roleData.First(y => y.Id == x.User.Roles.FirstOrDefault().RoleId).Name, RoleId = x.User.Roles.FirstOrDefault().RoleId
                    });
                }
                else
                {
                    var data = Context.Set <UserProfile>().Where(whereClause);
                    TotalCount = data.Count();
                    Data       = data.OrderByDescending(orderClause).Skip((CurrentPage - 1) * PageSize).Take(PageSize).ToList().Select(x => new UserModel {
                        FirstName = x.FirstName, Id = x.Id, LastName = x.LastName, UserName = x.User.UserName, Role = roleData.First(y => y.Id == x.User.Roles.FirstOrDefault().RoleId).Name, RoleId = x.User.Roles.FirstOrDefault().RoleId
                    });
                }
            }
            TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);



            // Object which we are going to send in header
            var paginationMetadata = new PagingReturnModel
            {
                TotalCount   = TotalCount,
                PageSize     = PageSize,
                CurrentPage  = CurrentPage,
                TotalPages   = TotalPages,
                PreviousPage = CurrentPage > 1,         // if CurrentPage is greater than 1 means it has previousPage
                NextPage     = CurrentPage < TotalPages // if TotalPages is greater than CurrentPage means it has nextPage
            };


            return(new Tuple <PagingReturnModel, IEnumerable <UserModel> >(paginationMetadata, Data));
        }
Ejemplo n.º 2
0
        public Tuple <PagingReturnModel, IEnumerable <TEntity> > Page(Func <TEntity, bool> whereClause, Func <TEntity, IComparable> orderClause, bool isAsc, int pageNumber, int pageSize)
        {
            int CurrentPage = pageNumber;
            int PageSize    = pageSize;
            int TotalCount  = 0;
            int TotalPages  = (int)Math.Ceiling(TotalCount / (double)PageSize);
            IEnumerable <TEntity> Data;

            if (orderClause == null)
            {
                var data = Context.Set <TEntity>().Where(whereClause);
                TotalCount = data.Count();
                Data       = data.Skip((CurrentPage - 1) * PageSize).Take(PageSize).ToList();
            }
            else
            {
                if (isAsc)
                {
                    var data = Context.Set <TEntity>().Where(whereClause);
                    TotalCount = data.Count();
                    Data       = data.OrderBy(orderClause).Skip((CurrentPage - 1) * PageSize).Take(PageSize).ToList();
                }
                else
                {
                    var data = Context.Set <TEntity>().Where(whereClause);
                    TotalCount = data.Count();
                    Data       = data.OrderByDescending(orderClause).Skip((CurrentPage - 1) * PageSize).Take(PageSize).ToList();
                }
            }
            TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);



            // Object which we are going to send in header
            var paginationMetadata = new PagingReturnModel
            {
                TotalCount   = TotalCount,
                PageSize     = PageSize,
                CurrentPage  = CurrentPage,
                TotalPages   = TotalPages,
                PreviousPage = CurrentPage > 1,         // if CurrentPage is greater than 1 means it has previousPage
                NextPage     = CurrentPage < TotalPages // if TotalPages is greater than CurrentPage means it has nextPage
            };


            return(new Tuple <PagingReturnModel, IEnumerable <TEntity> >(paginationMetadata, Data));
        }