Пример #1
0
        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="source">source</param>
        /// <param name="pageIndex">Page index</param>
        /// <param name="pageSize">Page size</param>
        public PagedList(IQueryable <T> source, GridPageModel pager)
        {
            if (!string.IsNullOrWhiteSpace(pager.sidx) && pager.sidx.Trim().Length > 0)
            {
                string sortingDir = string.Empty;
                if (pager.sord.ToUpper().Trim() == "ASC")
                {
                    sortingDir = "OrderBy";
                }
                else if (pager.sord.ToUpper().Trim() == "DESC")
                {
                    sortingDir = "OrderByDescending";
                }
                //ParameterExpression param = Expression.Parameter(typeof(T), sortExpression);
                //PropertyInfo pi = typeof(T).GetProperty(sortExpression);
                //Type[] types = new Type[2];
                //types[0] = typeof(T);
                //types[1] = pi.PropertyType;
                //Expression expr = Expression.Call(typeof(Queryable), sortingDir, types, source.Expression, Expression.Lambda(Expression.Property(param, sortExpression), param));
                //source = source.AsQueryable().Provider.CreateQuery<T>(expr);


                source = source.OrderBy(pager.sidx + " " + pager.sord);
            }

            int total = source.Count();

            this.TotalCount = total;
            this.TotalPages = total / pager.rows;

            if (total % pager.rows > 0)
            {
                TotalPages++;
            }

            this.PageSize  = pager.rows;
            this.PageIndex = pager.page;
            this.AddRange(source.Skip((pager.page - 1) * pager.rows).Take(pager.rows).ToList());
        }
Пример #2
0
        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="source">source</param>
        /// <param name="pageIndex">Page index</param>
        /// <param name="pageSize">Page size</param>
        public PagedList(List <T> source, GridPageModel pager)
        {
            var query = source.AsQueryable();

            if (pager.sidx.Trim().Length > 0)
            {
                string sortingDir = string.Empty;
                if (pager.sord.ToUpper().Trim() == "ASC")
                {
                    sortingDir = "OrderBy";
                }
                else if (pager.sord.ToUpper().Trim() == "DESC")
                {
                    sortingDir = "OrderByDescending";
                }
                ParameterExpression param = Expression.Parameter(typeof(T), pager.sidx);
                PropertyInfo        pi    = typeof(T).GetProperty(pager.sidx);
                Type[] types = new Type[2];
                types[0] = typeof(T);
                types[1] = pi.PropertyType;
                Expression expr = Expression.Call(typeof(Queryable), sortingDir, types, query.Expression, Expression.Lambda(Expression.Property(param, pager.sidx), param));
                query = query.AsQueryable().Provider.CreateQuery <T>(expr);
            }

            TotalCount = query.Count();
            TotalPages = TotalCount / pager.rows;

            if (TotalCount % pager.rows > 0)
            {
                TotalPages++;
            }

            this.PageSize  = pager.rows;
            this.PageIndex = pager.page;
            this.AddRange(query.Skip((pager.page - 1) * pager.rows).Take(pager.rows).ToList());
        }