/// <summary>
- /// 分页查询 + 条件查询 + 排序
- /// </summary>
- /// <typeparam name="Tkey">泛型</typeparam>
- /// <param name="pageSize">每页大小</param>
- /// <param name="pageIndex">当前页码</param>
- /// <param name="total">总数量</param>
- /// <param name="whereLambda">查询条件</param>
- /// <param name="orderbyLambda">排序条件</param>
- /// <param name="isAsc">是否升序</param>
- /// <returns>IQueryable 泛型集合</returns>
- public IQueryable<T> LoadPageItems<Tkey>(int pageSize, int pageIndex, out int total, Expression<Func<T, bool>> whereLambda, Func<T, Tkey> orderbyLambda, bool isAsc)
- {
- total = MyBaseDbContext.Set<T>().Where(whereLambda).Count();
- if (isAsc)
- {
- var temp = MyBaseDbContext.Set<T>().Where(whereLambda)
- .OrderBy<T, Tkey>(orderbyLambda)
- .Skip(pageSize * (pageIndex - 1))
- .Take(pageSize);
- return temp.AsQueryable();
- }
- else
- {
- var temp = MyBaseDbContext.Set<T>().Where(whereLambda)
- .OrderByDescending<T, Tkey>(orderbyLambda)
- .Skip(pageSize * (pageIndex - 1))
- .Take(pageSize);
- return temp.AsQueryable();
- }
- }
//查询要求:每页10条,显示第2页,查询性别为“男”,按年龄增序排列
int totalRecord;
List<Student>result = studentService.LoadItems(10,2,out totalRecord,u=>u.Sex==”男”,u=>u.Age,True);