EntityFramework进阶(五)- 分页

本系列原创博客代码已在EntityFramework6.0.0测试通过,转载请标明出处

我们创建分页信息类CommonPagedList,包含了字段总条数,总页数,当前页码,页大小,当前页数据。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/*
* CopyRight ©2017 All Rights Reserved
* 作者:Rex Sheng
*/
namespace SuperNet.EntityFramework.PagedList
{
    /// <summary>
    /// 共通分页类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class CommonPagedList<T> : List<T>
       where T : class
    {
        public int TotalItemCount { get; set; }

        public int PageSize { get; set; }


        public int PageIndex { get; set; }

        public int TotalPageCount { get; set; }

        private CommonPagedList() { }

        public CommonPagedList(CommonPagedList<T> list)
        {
            this.PageIndex = list.PageIndex;
            this.PageSize = list.PageSize;
            this.TotalItemCount = list.TotalItemCount;
            this.TotalPageCount = list.TotalPageCount;
            AddRange(list);
        }

        public CommonPagedList(IQueryable<T> list, int pageIndex, int pageSize)
        {
            if (pageIndex <= 0)
                pageIndex = 1;
            this.TotalItemCount = list.Count();
            this.PageSize = pageSize;
            this.TotalPageCount = this.TotalItemCount % this.PageSize == 0 ? this.TotalItemCount / this.PageSize : this.TotalItemCount / this.PageSize + 1;
            pageIndex = pageIndex > this.TotalPageCount ? this.TotalPageCount : pageIndex;
            this.PageIndex = pageIndex;
            if (this.PageIndex > this.TotalPageCount)
                this.PageIndex = this.TotalPageCount;
            if (pageIndex <= 0)
            {
                pageIndex = 1;
            }
            if (this.PageIndex <= 0)
            {
                this.PageIndex = 1;
            }

            var query = list.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            AddRange(query);
        }

        public CommonPagedList(IEnumerable<T> list, int pageIndex, int pageSize, int totalItemCount)
        {
            if (pageIndex <= 0)
                pageIndex = 1;
            this.TotalItemCount = totalItemCount;
            this.TotalPageCount = totalItemCount % pageSize == 0 ? totalItemCount / pageSize : totalItemCount / pageSize + 1;
            pageIndex = pageIndex > this.TotalPageCount ? this.TotalPageCount : pageIndex;
            if (pageIndex <= 0)
                pageIndex = 1;
            this.PageIndex = pageIndex;
            this.PageSize = pageSize;
            AddRange(list);
        }
    }
}

调用的时候,我们创建扩展方法来实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

/*
* CopyRight ©2017 All Rights Reserved
* 作者:Rex Sheng
*/
namespace SuperNet.EntityFramework.PagedList
{
    public static class PagedListExtension
    {
        /// <summary>
        /// 共通分页类扩展
        /// </summary>
        /// <typeparam name="T">查询返回类</typeparam>
        /// <param name="allItems">要查询的数据</param>
        /// <param name="pageIndex">要查询的页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <returns></returns>
        public static CommonPagedList<T> ToCommonPagedList<T>(this IOrderedQueryable<T> allItems, int pageIndex, int pageSize) where T : class
        {
            CommonPagedList<T> Result = new CommonPagedList<T>(allItems, pageIndex, pageSize);
            return Result;
        }

        /// <summary>
        /// 单个字段排序分页查询
        /// </summary>
        /// <typeparam name="T">查询返回类</typeparam>
        /// <typeparam name="TKey">排序字段类型</typeparam>
        /// <param name="allItems">要查询的数据</param>
        /// <param name="orderator">排序字段表达式</param>
        /// <param name="asc">是否升序</param>
        /// <param name="pageIndex">要查询的页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <returns></returns>
        public static CommonPagedList<T> ToCommonPagedList<T, TKey>(this IQueryable<T> allItems, Expression<Func<T, TKey>> orderator, bool asc, int pageIndex, int pageSize) where T : class
        {
            var source = asc ? allItems.OrderBy(orderator) : allItems.OrderByDescending(orderator);
            CommonPagedList<T> Result = new CommonPagedList<T>(source, pageIndex, pageSize);
            return Result;
        }

        /// <summary>
        /// 多个字段排序分页查询
        /// </summary>
        /// <typeparam name="T">查询返回类</typeparam>
        /// <typeparam name="TKey1">排序字段类型1</typeparam>
        /// <typeparam name="TKey2">排序字段类型2</typeparam>
        /// <param name="allItems">要查询的数据</param>
        /// <param name="orderator1">排序字段表达式1</param>
        /// <param name="orderator2">排序字段表达式2</param>
        /// <param name="asc1">是否升序1</param>
        /// <param name="asc2">是否升序2</param>
        /// <param name="pageIndex">要查询的页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <returns></returns>
        public static CommonPagedList<T> ToCommonPagedList<T, TKey1, TKey2>(this IQueryable<T> allItems, Expression<Func<T, TKey1>> orderator1, Expression<Func<T, TKey2>> orderator2, bool asc1, bool asc2, int pageIndex, int pageSize) where T : class
        {
            var source = asc1 ? (asc2 ? allItems.OrderBy(orderator1).ThenBy(orderator2) : allItems.OrderBy(orderator1).ThenByDescending(orderator2))
                : (asc2 ? allItems.OrderByDescending(orderator1).ThenBy(orderator2) : allItems.OrderByDescending(orderator1).ThenByDescending(orderator2));
            CommonPagedList<T> Result = new CommonPagedList<T>(source, pageIndex, pageSize);
            return Result;
        }

        /// <summary>
        /// 共通分页类
        /// </summary>
        /// <typeparam name="T">查询返回类</typeparam>
        /// <param name="allItems">要查询的数据</param>
        /// <param name="pageIndex">要查询的页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <returns></returns>
        public static CommonPagedList<T> ToCommonPagedList<T>(this IOrderedEnumerable<T> allItems, int pageIndex, int pageSize) where T : class
        {
            CommonPagedList<T> Result = new CommonPagedList<T>(allItems.AsQueryable(), pageIndex, pageSize);
            return Result;
        }
    }
}
原文地址:https://www.cnblogs.com/RexSheng/p/10928479.html