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();
}
}
}blog

調用:排序

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;
}ip

相關文章
相關標籤/搜索