EF:分页查询 + 条件查询 + 排序
/// <summary>
/// linq扩展类---zxh
/// </summary>
/// <typeparam name="T"></typeparam>
public class LinqExtend<T, Tkey> where T : class
{
/// <summary>
/// 分页查询 + 条件查询 + 排序
/// </summary>
/// <typeparam name="Tkey">泛型</typeparam>
/// <param name="pageSize">每页大小</param>
/// <param name="pageIndex">当前页码</param>
/// <param name="totalRecord">总数量</param>
/// <param name="whereLambda">查询条件</param>
/// <param name="orderbyLambda">排序条件</param>
/// <param name="isAsc">是否升序</param>
/// <returns>IQueryable 泛型集合</returns>
public static List<T> GetDataByPage(int pageIndex, int pageSize, out int totalRecord, Expression<Func<T, bool>> whereLambda, Func<T, Tkey> orderbyLambda, bool isAsc)
{
using (DreamOnlineEntities db = new DreamOnlineEntities())
{
totalRecord = db.Set<T>().Where(whereLambda).Count();
if (isAsc)
return db.Set<T>().Where(whereLambda).OrderBy(orderbyLambda).Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList();
else
return db.Set<T>().Where(whereLambda).OrderByDescending(orderbyLambda).Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList();
}
}
}
调用:
public List<AppUser> GetListPage()
{
int totalRecord = 0;
List<AppUser> list = LinqExtend<AppUser, string>.GetDataByPage(1, 10, out totalRecord, x => true, x => x.UserAccount, true);
return list;
}