zoukankan      html  css  js  c++  java
  • asp.net 分页类

    PaginatedList.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    namespace VML.Web.Mvc {
    public class PaginatedList<T> : List<T> {
    public int Offset { get; private set; }
    public int PageIndex { get; private set; }
    public int PageSize { get; private set; }
    public int TotalCount { get; private set; }
    public int TotalPages { get; private set; }

    /// <summary>
    /// For manually paing.
    /// </summary>
    /// <param name="source">The result of current page</param>
    /// <param name="totalCount"></param>
    /// <param name="pageIndex"></param>
    /// <param name="pageSize"></param>
    public PaginatedList(IEnumerable<T> source, int totalCount, int pageIndex, int pageSize)
    {
    Offset = pageIndex * pageSize;
    PageIndex = pageIndex;
    PageSize = pageSize;
    TotalCount = totalCount;
    TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);

    this.AddRange(source);
    }

    public PaginatedList(IQueryable<T> source, int pageIndex, int pageSize) {
    Offset = pageIndex * pageSize;
    PageIndex = pageIndex;
    PageSize = pageSize;
    TotalCount = source.Count();
    TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);

    this.AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));
    }

    public PaginatedList(IQueryable<T> source, int pageSize, int? offset = null, int? pageIndex = null) {
    if (offset != null) {
    Offset = offset.Value;
    PageIndex = (int)Math.Ceiling((decimal)Offset / pageSize);
    } else if (pageIndex != null) {
    Offset = pageIndex.Value * pageSize;
    PageIndex = pageIndex.Value;
    } else {
    //offset = null and pageIndex = null
    throw new ArgumentNullException("offset and pageIndex");
    }
    PageSize = pageSize;
    TotalCount = source.Count();
    TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);

    this.AddRange(source.Skip(Offset).Take(PageSize));
    }

    public bool HasPreviousPage {
    get {
    return (Offset > 0);
    }
    }

    public bool HasNextPage {
    get {
    return (Offset + PageSize < TotalCount);
    }
    }

    }
    }

  • 相关阅读:
    转:浅谈Linux的内存管理机制
    (转)Redis
    检测socket链接是否断开
    linux C 中的volatile使用
    LINUX 下 ipv6 socket 编程
    linux signal 列表
    Linux下异常信号
    linux signal
    转: 关于Linux常用的二进制文件分析方法
    IOI2020 题解
  • 原文地址:https://www.cnblogs.com/facial/p/4544415.html
Copyright © 2011-2022 走看看