/// <summary> /// 真分页方法 /// </summary> /// <typeparam name="T">要查询得 类型</typeparam> /// <typeparam name="Tkey">要排序的类型</typeparam> public class TruePagingHelp<T, Tkey> where T : class {
private testDbContext US_Context = new testDbContext(); /// DBContext public TruePagingHelp(Expression<Func<T, Boolean>> searchCondition, Expression<Func<T, Tkey>> condition, Boolean desc, int PageIndex, int pageSize) { Int32 count = 0; PageIndex = PageIndex <= 0 ? 1 : PageIndex; this.PageIndex = PageIndex; /// 第几页 this.PageSize = pageSize; /// 每页数据量 this.DataSource = this.GetData<T, Tkey>(searchCondition, condition, desc, PageIndex, pageSize, out count); this.PageCount = (int)Math.Ceiling(count / (Double)pageSize); /// 总共页数 } /// <summary> /// 分页源数据 /// </summary> public IEnumerable<T> DataSource { get; private set; } /// <summary> /// 每页记录数量 /// </summary> public int PageSize { get; private set; } /// <summary> /// 当前页数 /// </summary> public int PageIndex { get; set; } //分页总数 public int PageCount { get; private set; } public IEnumerable<T> GetData<T, Tkey>(Expression<Func<T, Boolean>> filterCondition, Expression<Func<T, Tkey>> sortCondition, Boolean desc, int PageIndex, int pageSize, out int total) where T : class { IEnumerable<T> list; total = US_Context.Set<T>().Where<T>(filterCondition).Count(); if (desc) { list = US_Context.Set<T>().Where<T>(filterCondition).OrderByDescending(sortCondition).Skip(pageSize * (PageIndex - 1)).Take(pageSize).ToList(); } else { list = US_Context.Set<T>().Where<T>(filterCondition).OrderBy(sortCondition).Skip(pageSize * (PageIndex - 1)).Take(pageSize).ToList(); } return list; } }
其中使用了, 泛型、泛型约束等;
写了EF 真分页 会EF查询效率有很了解,值得自己去写;