热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

ASP.NETMVC4分页组合查询解决方法

本人新手刚在webform转到mvc像linqef啥的,都是不会的不行不行的,不会就问群友,找资料今天本屌遇到了一个分页组合查询的问题&#

本人新手刚在webform转到mvc   像linq  ef啥的,都是不会的不行不行的,不会就问群友,找资料

今天本屌遇到了一个分页组合查询的问题,解决了2个小时,把代码共享给大家

话不多话,直接上代码,理论上的东东我就不描述,初中没毕业文化太低也描述不了

 

新建一个表达式数的操作类

public static class PredicateBuilder{public static Expressionbool>> True() { return f => true; }public static Expressionbool>> False() { return f => false; }public static Expression Compose(this Expression first, Expression second, Func 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 firstvar secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);// apply composition of lambda expression bodies to parameters from the first expression return Expression.Lambda(merge(first.Body, secondBody), first.Parameters);}public static Expressionbool>> And(this Expressionbool>> first, Expressionbool>> second){return first.Compose(second, Expression.And);}public static Expressionbool>> Or(this Expressionbool>> first, Expressionbool>> second){return first.Compose(second, Expression.Or);}}public class ParameterRebinder : ExpressionVisitor{private readonly Dictionary map;public ParameterRebinder(Dictionary map){this.map = map ?? new Dictionary();}public static Expression ReplaceParameters(Dictionary 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);}}

View Code

 

基本的做好,下一步就是调用了

代码  下看

//获取页容量int pageSize &#61; FytRequest.GetFormInt("rows", 10);//获取请求的页码int pageIndex &#61; FytRequest.GetFormInt("page", 1);var key &#61; FytRequest.GetFormStringEncode("key");int statu &#61; FytRequest.GetFormInt("statu");var begintime &#61; FytRequest.GetFormString("begintime");var endtime &#61; FytRequest.GetFormString("endtime");var where &#61; PredicateBuilder.True();#region 组合查询if (key!&#61;""){where &#61; where.And(m &#61;> m.loginName.Contains(key));where &#61; where.Or(m&#61;>m.title.Contains(key));}if (statu!&#61;-1){where &#61; where.And(m&#61;>m.logType&#61;&#61;statu);}if (begintime !&#61; "" && endtime !&#61; ""){var bt &#61; Convert.ToDateTime(begintime);var et &#61; Convert.ToDateTime(endtime);where &#61; where.And(m &#61;> m.addDate >&#61; bt && m.addDate <&#61; et);}#endregionvar dataList &#61; OperateContext.BLLSession.Itb_SystemLogBLL.GetPagedList(pageIndex, pageSize, where, p &#61;> p.ID);int rowCount &#61; OperateContext.BLLSession.Itb_SystemLogBLL.GetListBy(where).Count();var jsonM &#61; new JsonHelper.JsonAjaxModel(){Data &#61; dataList,PageTotal &#61; 0,Status &#61; "ok"};

View Code

 

ok好了&#xff0c;这样就可以用了&#xff0c;我这个分页内部是接口做的&#xff0c;

爽来的&#xff0c;我把分页实现也分享给大家&#xff0c; 

#region 6.0 分页查询 &#43; List GetPagedList///

/// 6.0 分页查询 &#43; List GetPagedList/// /// 页码/// 页容量/// 条件 lambda表达式/// 排序 lambda表达式/// public List GetPagedList(int pageIndex, int pageSize, Expressionbool>> whereLambda, Expression> orderBy){// 分页 一定注意&#xff1a; Skip 之前一定要 OrderByreturn whereLambda &#61;&#61; null ? db.Set().OrderBy(orderBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList() : db.Set().Where(whereLambda).OrderBy(orderBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();}#endregion
View Code

有些人可能看代码很熟悉&#xff0c;就不要拆穿了&#xff0c;你懂得

交流&#xff1a;86594082 有事情可以找我哈&#xff0c;共同学习

 

转:https://www.cnblogs.com/fuyu-blog/p/4347447.html



推荐阅读
author-avatar
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有