コード例 #1
0
        public static IQueryable ToPagedList(this IQueryable source, PageCtl pageCtl)
        {
            if (pageCtl == null)
            {
                return(source);
            }

            if (pageCtl.PageSize == 0)
            {
                pageCtl.PageSize = 30;
            }
            if (pageCtl.PageIndex == 0)
            {
                pageCtl.PageIndex = 1;
            }


            int total = source.Count();

            pageCtl.TotalCount = total;
            pageCtl.TotalPages = total / pageCtl.PageSize;


            if (total % pageCtl.PageSize > 0)
            {
                pageCtl.TotalPages++;
            }

            string OrderByField = pageCtl.OrderByField;
            bool   Ascending    = !pageCtl.Dscending;

            Type type = source.ElementType;

            if (string.IsNullOrEmpty(OrderByField))
            {
                PropertyInfo[] propertys = type.GetProperties();
                if (propertys.Length == 0)
                {
                    throw new Exception("当前实体未包含属性!");
                }
                var pIs = from c in propertys where c.GetCustomAttributes(false).Count() > 0 select c;
                var pI  = from c in pIs where c.CustomAttributes.Where(w => w.AttributeType == typeof(System.ComponentModel.DataAnnotations.KeyAttribute)).Count() > 0 select c;
                if (pI.Count() > 0)
                {
                    OrderByField = pI.First().Name;
                }
                else
                {
                    OrderByField = propertys[0].Name;
                }
            }

            PropertyInfo property = type.GetProperty(OrderByField);

            if (property == null)
            {
                throw new Exception("错误:\r\n当前实体中不存在指定的排序字段:" + OrderByField);
            }

            ParameterExpression param = Expression.Parameter(type, "p");
            Expression          propertyAccessExpression = Expression.MakeMemberAccess(param, property);
            LambdaExpression    orderByExpression        = Expression.Lambda(propertyAccessExpression, param);

            string methodName = Ascending ? "OrderBy" : "OrderByDescending";

            MethodCallExpression resultExp = Expression.Call(
                typeof(Queryable), methodName, new Type[] { type, property.PropertyType },
                source.Expression,
                Expression.Quote(orderByExpression)
                );

            var query = source.Provider.CreateQuery(resultExp);

            var PageSize = pageCtl.PageSize;

            if (pageCtl.PageIndex > pageCtl.TotalPages)
            {
                pageCtl.PageIndex = pageCtl.TotalPages;
            }
            if (pageCtl.PageIndex < 1)
            {
                pageCtl.PageIndex = 1;
            }
            //  this.PageIndex = pageCtl.PageIndex;
            //pageCtl.TotalPages = this.TotalPages;
            //pageCtl.TotalCount = this.TotalCount;

            query = query._Skip((pageCtl.PageIndex - 1) * pageCtl.PageSize)._Take(pageCtl.PageSize);
            return(query);
        }
コード例 #2
0
        public PagedList(IQueryable <T> source, PageCtl pageCtl)
        {
            if (pageCtl == null)
            {
                this.AddRange(source.ToList());
                this.ToList();
                return;
            }

            if (pageCtl.PageSize == 0)
            {
                pageCtl.PageSize = 30;
            }
            if (pageCtl.PageIndex == 0)
            {
                pageCtl.PageIndex = 1;
            }


            int total = source.Count();

            this.TotalCount = total;
            this.TotalPages = total / pageCtl.PageSize;

            if (total % pageCtl.PageSize > 0)
            {
                TotalPages++;
            }

            string OrderByField = pageCtl.OrderByField;
            bool   Ascending    = !pageCtl.Dscending;

            Type type = typeof(T);

            if (string.IsNullOrEmpty(OrderByField))
            {
                PropertyInfo[] propertys = type.GetProperties();
                if (propertys.Length == 0)
                {
                    throw new Exception("当前实体未包含属性!");
                }
                //OrderByField = propertys[0].Name;
                foreach (var item in propertys.Where(x => x.GetCustomAttributes(false).Count() > 0))
                {
                    if (item.CustomAttributes.Where(x => x.AttributeType == new System.ComponentModel.DataAnnotations.KeyAttribute().GetType()).FirstOrDefault() != null)
                    {
                        OrderByField = item.Name;
                        break;
                    }
                }
            }

            PropertyInfo property = type.GetProperty(OrderByField);

            if (property == null)
            {
                throw new Exception("错误:\r\n当前实体中不存在指定的排序字段:" + OrderByField);
            }


            ParameterExpression param = Expression.Parameter(type, "p");
            Expression          propertyAccessExpression = Expression.MakeMemberAccess(param, property);
            LambdaExpression    orderByExpression        = Expression.Lambda(propertyAccessExpression, param);

            string methodName = Ascending ? "OrderBy" : "OrderByDescending";

            MethodCallExpression resultExp = Expression.Call(
                typeof(Queryable), methodName, new Type[] { type, property.PropertyType },
                source.Expression,
                Expression.Quote(orderByExpression)
                );

            var query = source.Provider.CreateQuery <T>(resultExp);



            this.PageSize = pageCtl.PageSize;
            if (pageCtl.PageIndex > this.TotalPages)
            {
                pageCtl.PageIndex = this.TotalPages;
            }
            if (pageCtl.PageIndex < 1)
            {
                pageCtl.PageIndex = 1;
            }
            this.PageIndex     = pageCtl.PageIndex;
            pageCtl.TotalPages = this.TotalPages;
            pageCtl.TotalCount = this.TotalCount;

            var list = query.Skip((pageCtl.PageIndex - 1) * pageCtl.PageSize).Take(pageCtl.PageSize).ToList();

            this.AddRange(list);
            this.ToList();
        }
コード例 #3
0
 public static List <T> ToPagedList <T>(this IEnumerable <T> linq, PageCtl pcl)
 {
     return(new PagedList <T>(linq, pcl).ToList());
 }