分享一个 EF6 分页查询数据的 IQueryable 扩展方法-使用例子

时间:2024-07-10 08:58:52
  1. 方法一的使用例子

    public List<T> QueryPageList<S>(int pageIndex, int pageSize, Expression<Func<T, bool>> whereLambda, Expression<Func<T, S>> orderbyLambda, out int total, bool isAsc)
    {
    	var source = _dbContext.Set<T>().AsNoTracking().AsQueryable();
    	var result = source.FindPageList(pageIndex, pageSize, whereLambda, orderbyLambda, out total, isAsc);
    	return result; 
    }
    
    public List<TB01Dto> QueryPageList(QueryPageParams queryPageParams, out int total)
    {
    	Expression<Func<TB01, bool>> whereLambda = a => !a.STSHT01.Equals("D", StringComparison.OrdinalIgnoreCase);
    	if (!string.IsNullOrEmpty(queryPageParams.SearchKeyword))
    	{
    		Expression<Func<TB01, bool>> second = (a => a.NAMEHT01.Contains(queryPageParams.SearchKeyword) || a.COMPHT01 == queryPageParams.SearchKeyword);
    		whereLambda = whereLambda.And(second);
    	}
    	var list = QueryPageList(queryPageParams.PageIndex, queryPageParams.PageSize, whereLambda, (a => a.COMPHT01), out total, queryPageParams.IsAsc);
    	var result = CommonUtil.TranObject2OtherType<List<TB01Dto>>(list);
    	return result;
    }
    
  2. 方法二的使用例子

    public List<UserMenuDTO> QueryPageList(QueryPageParamsForuserMenu queryPageParams, out int total)
    {
    	 var query = userMenuReposition.QueryMenuUsers(queryPageParams.MenuUserName);
    	 
    	 Expression<Func<UserMenuDTO, bool>> whereLambda = a => true;
    	 
    	 if (queryPageParams.CompanyCodes != null && queryPageParams.CompanyCodes.Length > 0)
    	 {
    		Expression<Func<UserMenuDTO, bool>> second = (a => queryPageParams.CompanyCodes.Contains(a.COMPHT03));
    		whereLambda = whereLambda.And(second); 
    	 }
    	 
    	 var list = query.FindPageList(queryPageParams.PageIndex, queryPageParams.PageSize, whereLambda, "COMPHT03, MNUCDHT03", out total);
    	 
    	 return list;
    }