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);
    }
    }
    }

  • 相关阅读:
    前端基础之HTML(第一天)
    前端基础之CSS(第二天)
    03bootstrap_表格
    02bootstrap_通用标签样式和代码
    01bootstrap_基本结构和布局
    python学习之路08——元组、集合
    python学习之路07
    python学习之路06——字符串
    python学习之路05
    python学习之路04——列表和字典
  • 原文地址:https://www.cnblogs.com/niuzaihenmang/p/5614772.html
Copyright © 2011-2022 走看看