你的位置:首页 > ASP.net教程

[ASP.net教程]自己试试写个分页类


分页接口

 public interface IPager<TEntity,TKey> where TEntity :class  {    /// <summary>    /// 数据总量(行数)    /// </summary>    int PageTotal { get; }    /// <summary>    /// 当前页码    /// </summary>    int PageIndex { get; }    /// <summary>    /// 当前页的数据量(行数)    /// </summary>    int PageSize { get; }    /// <summary>    /// 总页数    /// </summary>    int Count { get; }    /// <summary>    /// 跳过的数据量(行数)    /// </summary>    int Skip { get; }    /// <summary>    /// 选择的数据量(行数)    /// </summary>    int Take { get; }    /// <summary>    /// 设置每页数量    /// </summary>    /// <param name="size">默认10条</param>    void SetPageSize(int size = 10);    /// <summary>    /// 获取指定页的所有实体数据,并按TKey排序    /// </summary>    /// <param name="pageIndex"></param>    /// <param name="order"></param>    /// <returns></returns>    IQueryable<TEntity> GetPageList(int pageIndex, Expression<Func<TEntity, TKey>> order);  }

为了数据安全,全部用的只读属性,通过设置每页数量,其它数据自动计算出来,实现类是用的EF。后面那个Expression好像写错了,希望有同志指点一下。下面是实现类

using OADemo.Common;using OADemo.Datas.Abstracts;using System;using System.Data.Entity;using System.Linq;using System.Linq.Expressions;namespace OADemo.Datas.EFDatas{  public class Pager<TEntity, TKey>:IPager<TEntity,TKey> where TEntity:class  {    public Pager()    {      pageTotal = unitOfWork.Set<TEntity>().Count();      SetPageSize();    }    private static DbContext unitOfWork = ServiceContainer.Resolve<DbContext>();    public int PageTotal { get { return pageTotal; } }    private int pageTotal;    public int PageIndex { get { return index; } }//指定页码数    private int index;    public int PageSize { get { return pageSize; } }//每页数量    private int pageSize;    public int Count { get { return count; } }//总页数    private int count;    public int Skip { get { return skip; } }    private int skip;    public int Take { get { return take; } }    private int take;    public void SetPageSize(int size = 10)    {      if (size <= 0)        size = 10;      if (PageIndex <= 0)        index = 1;      if (pageTotal <= size)      {        index = 1; count = 1; pageSize = pageTotal; skip = 0; take = pageTotal;      }      else      {        int n = pageTotal % size;        if (n == 0)        {          count = pageTotal / size;          if (PageIndex > count)          {            index = count;          }          pageSize = size; take = size; skip = (index - 1) * size;        }        if (n > 0)        {          count = pageTotal / size + 1;          if (PageIndex > count)          {            index = count; pageSize = n; skip = (index - 1) * size; take = n;          }          if (PageIndex == count)          {            pageSize = n; skip = (index - 1) * size; take = n;          }          else if (PageIndex < count)          {            pageSize = size; take = size; skip = (index - 1) * size;          }        }      }    }    public IQueryable<TEntity> GetPageList(int pageIndex, Expression<Func<TEntity, TKey>> order)    {      this.index = pageIndex;      SetPageSize(PageSize);      return unitOfWork.Set<TEntity>().OrderBy(order).Skip(skip).Take(take);    }  }}

View Code