示例#1
0
        public virtual List <T> PagingList(
            out int itemsCount,
            Expression <Func <T, bool> > filter = null,
            string includedProperties           = "",
            int currentPage       = 1,
            int countPerPage      = 10,
            string order          = "",
            string orderDirection = ""
            )
        {
            if (countPerPage == 0)
            {
                countPerPage = 10;
            }

            if (currentPage == 0)
            {
                currentPage = 1;
            }

            itemsCount = 0;
            int            skip  = (currentPage - 1) * countPerPage;
            IQueryable <T> query = DBSet;

            if (filter != null)
            {
                query = query.Where(filter);
            }

            if (!string.IsNullOrWhiteSpace(includedProperties))
            {
                query = includedProperties
                        .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                        .Aggregate(query, (current, includeProperty) => current.Include(includeProperty));
            }

            itemsCount = query.Count();

            if (!string.IsNullOrWhiteSpace(order))
            {
                if (orderDirection.Trim().ToUpper() == "DESC")
                {
                    query = OrderDynamic.OrderByDescending(query, order);
                }
                else
                {
                    query = OrderDynamic.OrderBy(query, order);
                }
            }
            else
            {
                query = OrderDynamic.OrderBy(query, "ID");
            }

            query = query.Skip(skip).Take(countPerPage);

            return(query.ToList());
        }
示例#2
0
        public virtual List <T> PagingList(
            out int itemsCount,
            IQueryable <T> query      = null,
            string includedProperties = "",
            int currentPage           = 1,
            int countPerPage          = 10,
            Func <IQueryable <T>,
                  IOrderedQueryable <T> > order = null,
            bool defaultOrder = false
            )
        {
            itemsCount = 0;
            int skip = (currentPage - 1) * countPerPage;

            if (query == null)
            {
                query = DBSet;
            }

            if (!string.IsNullOrWhiteSpace(includedProperties))
            {
                query = includedProperties
                        .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                        .Aggregate(query, (current, includeProperty) => current.Include(includeProperty));
            }

            itemsCount = query.Count();

            if (order != null)
            {
                query = order(query);
            }
            else if (!defaultOrder)
            {
                query = OrderDynamic.OrderBy(query, "ID");
            }

            query = query.Skip(skip).Take(countPerPage);

            return(query.ToList());
        }