基于领域驱动设计(DDD)超轻量级快速开发架构(二)动态linq查询的实现方式

-之动态查询,查询逻辑封装复用

基于领域驱动设计(DDD)超轻量级快速开发架构详细介绍请看

https://www.cnblogs.com/neozhu/p/13174234.html

需求

配合EasyUI datagird filter实现多字段(任意字段)的筛选

根据业务需求筛选特定的状态或条件,如:查看结案的订单,最近30天的订单,查看属于我的订单.等等,这些逻辑是固定也是可以被重用,但又不想每次写相同的条件,那么下面我会给我的解决方案.

需求1只是一个偷懒的实现方式,因为datagrid自带这个功能,但又不想根据具体的需求来画查询条件,如果需求必须要再datagrid上面做一块查询条件的输入那目前只能在前端自己手工添加,在组织后传入后台,暂时不在这里讨论

需求2可能不太好解释,看完代码就自然理解为什么要这么做了,这么做的好处有哪些

具体实现的方式

基于领域驱动设计(DDD)超轻量级快速开发架构(二)动态linq查询的实现方式

 

 默认情况下 datagrid 有几列就可以对这几列进行筛选,对于日期型的字段会采用between,选择2个时间之间进行筛选,数字类型会提供大于小于等符号选择,可以自行尝试,其原理是datagrid 会根据datagrid 头部输入的值生成一个Json字符串发送后台请求数据

JSON:格式

filterRules: [
{field:field,op:op,value:value}, {field:field,op:op,value:value}, ]

通常的做法是一个一个判断加条件

基于领域驱动设计(DDD)超轻量级快速开发架构(二)动态linq查询的实现方式

基于领域驱动设计(DDD)超轻量级快速开发架构(二)动态linq查询的实现方式

1 var filters = JsonConvert.DeserializeObject<IEnumerable<filterRule>>(filterRules); 2 foreach (var rule in filters) 3 { 4 if (rule.field == "Id" && !string.IsNullOrEmpty(rule.value) && rule.value.IsInt()) 5 { 6 var val = Convert.ToInt32(rule.value); 7 switch (rule.op) 8 { 9 case "equal": 10 this.And(x => x.Id == val); 11 break; 12 case "notequal": 13 this.And(x => x.Id != val); 14 break; 15 case "less": 16 this.And(x => x.Id < val); 17 break; 18 case "lessorequal": 19 this.And(x => x.Id <= val); 20 break; 21 case "greater": 22 this.And(x => x.Id > val); 23 break; 24 case "greaterorequal": 25 this.And(x => x.Id >= val); 26 break; 27 default: 28 this.And(x => x.Id == val); 29 break; 30 } 31 } 32 if (rule.field == "Name" && !string.IsNullOrEmpty(rule.value)) 33 { 34 this.And(x => x.Name.Contains(rule.value)); 35 } 36 if (rule.field == "Code" && !string.IsNullOrEmpty(rule.value)) 37 { 38 this.And(x => x.Code.Contains(rule.value)); 39 } 40 41 if (rule.field == "Address" && !string.IsNullOrEmpty(rule.value)) 42 { 43 this.And(x => x.Address.Contains(rule.value)); 44 } 45 46 if (rule.field == "Contect" && !string.IsNullOrEmpty(rule.value)) 47 { 48 this.And(x => x.Contect.Contains(rule.value)); 49 } 50 51 if (rule.field == "PhoneNumber" && !string.IsNullOrEmpty(rule.value)) 52 { 53 this.And(x => x.PhoneNumber.Contains(rule.value)); 54 } 55 56 if (rule.field == "RegisterDate" && !string.IsNullOrEmpty(rule.value)) 57 { 58 if (rule.op == "between") 59 { 60 var datearray = rule.value.Split(new char[] { '-' }); 61 var start = Convert.ToDateTime(datearray[0]); 62 var end = Convert.ToDateTime(datearray[1]); 63 64 this.And(x => SqlFunctions.DateDiff("d", start, x.RegisterDate) >= 0); 65 this.And(x => SqlFunctions.DateDiff("d", end, x.RegisterDate) <= 0); 66 } 67 } 68 if (rule.field == "CreatedDate" && !string.IsNullOrEmpty(rule.value)) 69 { 70 if (rule.op == "between") 71 { 72 var datearray = rule.value.Split(new char[] { '-' }); 73 var start = Convert.ToDateTime(datearray[0]); 74 var end = Convert.ToDateTime(datearray[1]); 75 76 this.And(x => SqlFunctions.DateDiff("d", start, x.CreatedDate) >= 0); 77 this.And(x => SqlFunctions.DateDiff("d", end, x.CreatedDate) <= 0); 78 } 79 } 80 81 82 if (rule.field == "CreatedBy" && !string.IsNullOrEmpty(rule.value)) 83 { 84 this.And(x => x.CreatedBy.Contains(rule.value)); 85 } 86 87 if (rule.field == "LastModifiedDate" && !string.IsNullOrEmpty(rule.value)) 88 { 89 if (rule.op == "between") 90 { 91 var datearray = rule.value.Split(new char[] { '-' }); 92 var start = Convert.ToDateTime(datearray[0]); 93 var end = Convert.ToDateTime(datearray[1]); 94 95 this.And(x => SqlFunctions.DateDiff("d", start, x.LastModifiedDate) >= 0); 96 this.And(x => SqlFunctions.DateDiff("d", end, x.LastModifiedDate) <= 0); 97 } 98 } 99 100 if (rule.field == "LastModifiedBy" && !string.IsNullOrEmpty(rule.value)) 101 { 102 this.And(x => x.LastModifiedBy.Contains(rule.value)); 103 } 104 105 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpfxzd.html