zoukankan      html  css  js  c++  java
  • MVC分页控件之二,为IQueryable定义一个扩展方法,直接反回PagedList<T>结果集

    namespace Entity
    {
        public interface IPagedList
        {
            /// <summary>
            /// 记录数
            /// </summary>
            int TotalCount { get; set; }
            /// <summary>
            /// 页数
            /// </summary>
            int TotalPages { get; set; }
            /// <summary>
            /// 当前页
            /// </summary>
            int PageIndex { get; set; }
            /// <summary>
            /// 页面大小
            /// </summary>
            int PageSize { get; set; }
            /// <summary>
            /// 是否上一页
            /// </summary>
            bool IsPreviousPage { get; }
            /// <summary>
            /// 是否下一页
            /// </summary>
            bool IsNextPage { get; }
        }
        /// <summary>
        /// 分页通用类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public class PagedList<T> : List<T>, IPagedList
        {
            /// <summary>
            /// 数据源为IQueryable的范型
            /// </summary>
            /// <param name="source">数据源</param>
            /// <param name="index">当前页</param>
            /// <param name="pageSize">每页显示多少条记录</param>
            public PagedList(IQueryable<T> source, int index, int pageSize)
            {
                if (source != null) //判断传过来的实体集是否为空
                {
                    int total = source.Count();
                    this.TotalCount = total;
                    this.TotalPages = total / pageSize;

                    if (total % pageSize > 0)
                        TotalPages++;

                    this.PageSize = pageSize;
                    if (index > this.TotalPages)
                    {
                        index = this.TotalPages;
                    }
                    if (index < 1)
                    {
                        index = 1;
                    }
                    this.PageIndex = index;
                    this.AddRange(source.Skip((index - 1) * pageSize).Take(pageSize).ToList()); //Skip是跳到第几页,Take返回多少条
                }
            }

            /// <summary>
            /// 总页数
            /// </summary>
            public int TotalPages { get; set; }
            /// <summary>
            /// 总记录数
            /// </summary>
            public int TotalCount { get; set; }
            /// <summary>
            /// 当前页
            /// </summary>
            public int PageIndex { get; set; }
            /// <summary>
            /// 每页显示多少条记录
            /// </summary>
            public int PageSize { get; set; }
            /// <summary>
            /// 是否有上一页
            /// </summary>
            public bool IsPreviousPage { get { return (PageIndex > 0); } }
            /// <summary>
            /// 是否有下一页
            /// </summary>
            public bool IsNextPage { get { return (PageIndex * PageSize) <= TotalCount; } }

            private NameValueCollection _AddParameters = new NameValueCollection();
            /// <summary>
            /// 分页参数
            /// </summary>
            public NameValueCollection AddParameters
            {
                get { return _AddParameters; }
                set { _AddParameters = value; }
            }

        }

        /// <summary>
        /// PagedList扩展类
        /// </summary>
        public static class ExtendPagedList
        {
            /// <summary>
            ///  返回PagedList<T>类型的IQueryable的扩展方法
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="linq"></param>
            /// <param name="pageIndex"></param>
            /// <param name="pageSize"></param>
            /// <returns></returns>
            public static PagedList<T> ToPagedList<T>(this IQueryable<T> linq, int pageIndex, int pageSize)
            {
                return new PagedList<T>(linq, pageIndex, pageSize);
            }
        }
    }

  • 相关阅读:
    drop table 、delete table和truncate table的区别
    润乾报表 删除导出excel弹出框里的选项
    学习笔记: 委托解析和封装,事件及应用
    学习笔记: 特性Attribute详解,应用封装
    学习笔记: 反射应用、原理,完成扩展,emit动态代码
    学习笔记: 泛型应用、原理、协变逆变、泛型缓存
    jmeter4.x centos7部署笔记
    rabbitmq3.7.5 centos7 集群部署笔记
    rabbitmq3.8.3 centos7 安装笔记
    UVA-12436 Rip Van Winkle's Code (线段树区间更新)
  • 原文地址:https://www.cnblogs.com/lori/p/2185687.html
Copyright © 2011-2022 走看看