ASP.NETMVC4 分页组合查询解决方法
本人新手刚在webform转到mvc 像linq ef啥的,都是不会的不行不行的,不会就问群友,找资料
今天本屌遇到了一个分页组合查询的问题,解决了2个小时,把代码共享给大家
话不多话,直接上代码,理论上的东东我就不描述,初中没毕业文化太低也描述不了
新建一个表达式数的操作类
public static class PredicateBuilder { public static Expression<Func<T, bool>> True<T>() { return f => true; } public static Expression<Func<T, bool>> False<T>() { return f => false; } public static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge) { // build parameter map (from parameters of second to parameters of first) var map = first.Parameters.Select((f, i) => new { f, s = second.Parameters[i] }).ToDictionary(p => p.s, p => p.f); // replace parameters in the second lambda expression with parameters from the first var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body); // apply composition of lambda expression bodies to parameters from the first expression return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters); } public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second) { return first.Compose(second, Expression.And); } public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second) { return first.Compose(second, Expression.Or); } } public class ParameterRebinder : ExpressionVisitor { private readonly Dictionary<ParameterExpression, ParameterExpression> map; public ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map) { this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>(); } public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp) { return new ParameterRebinder(map).Visit(exp); } protected override Expression VisitParameter(ParameterExpression p) { ParameterExpression replacement; if (map.TryGetValue(p, out replacement)) { p = replacement; } return base.VisitParameter(p); } }
基本的做好,下一步就是调用了
代码 下看
//获取页容量 int pageSize = FytRequest.GetFormInt("rows", 10); //获取请求的页码 int pageIndex = FytRequest.GetFormInt("page", 1); var key = FytRequest.GetFormStringEncode("key"); int statu = FytRequest.GetFormInt("statu"); var begintime = FytRequest.GetFormString("begintime"); var endtime = FytRequest.GetFormString("endtime"); var where = PredicateBuilder.True<tb_SystemLog>(); #region 组合查询 if (key!="") { where = where.And(m => m.loginName.Contains(key)); where = where.Or(m=>m.title.Contains(key)); } if (statu!=-1) { where = where.And(m=>m.logType==statu); } if (begintime != "" && endtime != "") { var bt = Convert.ToDateTime(begintime); var et = Convert.ToDateTime(endtime); where = where.And(m => m.addDate >= bt && m.addDate <= et); } #endregion var dataList = OperateContext.BLLSession.Itb_SystemLogBLL.GetPagedList(pageIndex, pageSize, where, p => p.ID); int rowCount = OperateContext.BLLSession.Itb_SystemLogBLL.GetListBy(where).Count(); var jsonM = new JsonHelper.JsonAjaxModel() { Data = dataList, PageTotal = 0, Status = "ok" };
ok好了,这样就可以用了,我这个分页内部是接口做的,
爽来的,我把分页实现也分享给大家,
#region 6.0 分页查询 + List<T> GetPagedList<TKey> /// <summary> /// 6.0 分页查询 + List<T> GetPagedList<TKey> /// </summary> /// <param name="pageIndex">页码</param> /// <param name="pageSize">页容量</param> /// <param name="whereLambda">条件 lambda表达式</param> /// <param name="orderBy">排序 lambda表达式</param> /// <returns></returns> public List<T> GetPagedList<TKey>(int pageIndex, int pageSize, Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> orderBy) { // 分页 一定注意: Skip 之前一定要 OrderBy return whereLambda == null ? db.Set<T>().OrderBy(orderBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList() : db.Set<T>().Where(whereLambda).OrderBy(orderBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); } #endregion
有些人可能看代码很熟悉,就不要拆穿了,你懂得
交流:86594082 有事情可以找我哈,共同学习