组合查询功能实现

前言

  这是我的第二篇文章,这是我之前做的ERP项目的时候设计实现的。在这个ERP系统中,功能比较多,表设计的时候建立了很多业务表。对于一些业务表需要执行很多查询,客户要求针对不同的字段进行查询,基于我们之前的设计,针对不同的查询条件设计不同的DAL方法,通过不同的方法签名来实现客户的对于不同条件查询的要求。但是这种解决方案会让程序员很被动,久而久之整个DAL层会显得很臃肿。

  面对这样的困境,考虑是否可以实现用一个通用的DAL方法来代替所有的不同筛选条件查询方法,因为这些查询方法内部的逻辑是一样的,只有查询条件不一样。

组合查询实现

  组合查询的意思就是,DAL方法不需要知道客户的具体查询条件是什么,就可以向客户返回一个结果集。要实现组合查询需要实现以下几个步骤:

  (1) 创建工具类PredicateExtensionses

 1     public static class PredicateExtensionses
 2     {
 3         public static Expression<Func<T, bool>> True<T>() { return f => true; }
 4 
 5         public static Expression<Func<T, bool>> False<T>() { return f => false; }
 6 
 7         public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> exp_flow, Expression<Func<T, bool>> expression2)
 8         {
 9 
10             var invokedExpression = System.Linq.Expressions.Expression.Invoke(expression2, exp_flow.Parameters.Cast<System.Linq.Expressions.Expression>());
11 
12             return System.Linq.Expressions.Expression.Lambda<Func<T, bool>>(System.Linq.Expressions.Expression.Or(exp_flow.Body, invokedExpression), exp_flow.Parameters);
13 
14         }
15 
16         public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> exp_flow, Expression<Func<T, bool>> expression2)
17         {
18 
19             var invokedExpression = System.Linq.Expressions.Expression.Invoke(expression2, exp_flow.Parameters.Cast<System.Linq.Expressions.Expression>());
20 
21             return System.Linq.Expressions.Expression.Lambda<Func<T, bool>>(System.Linq.Expressions.Expression.And(exp_flow.Body, invokedExpression), exp_flow.Parameters);
22 
23         }
24     }
PredicateExtensionses

  (2) 将查询条件封装成Model类

1     public class Filter
2     {
3         public string filter1 { get; set; }
4 
5         public string filter2 { get; set; }
6 
7         public string filter3 { get; set; }
8     }
Model

  (3) 组合查询方法

  组合查询的时候,需要对客户提供的查询条件做一个处理,将有效的查询的条件进行组装,有效是指string类型的变量不为空或者null、int类型变量值不为0等等。

 1 //Model是使用Linq to SQL生成的数据表对应的数据类名
 2 public void GetResultsByFilter(Filter filter, out IList<Model> results)
 3 {
 4  using (var dc = new DataContext())
 5                     {
 6                         results = new List<Model>();
 7                         Expression<Func<Model, bool>> expr = PredicateExtensionses.True<Model>();
 8                         //获得Model查询条件
 9                  GetFilterCondition(filter, ref expr);
10                         results = (from c in dc.Model.Where(expr) select c).ToList();
11               }
12 }
13 
14 //组合查询条件
15 private void GetFilterCondition(Filter filter, ref Expression<Func<Model, bool>> expr)
16         {
17             if (!string.IsNullOrEmpty(filter.filter1))
18             {
19                 expr = expr.And(c => (c.filter1== Filter.filter1));//1次组合 
20             }
21             if (!string.IsNullOrEmpty(filter.filter2))
22             {
23                 expr = expr.And(c => (c.filter2== Filter.filter2)); //2次组合 
24             }
25             if (!string.IsNullOrEmpty(filter.filter3))
26             {
27                 expr = expr.And(c => (c.filter3== Filter.filter3)); //3次组合 
28             }        
29 }
GetResultsByFilter

  方法签名中的Filter类是自定义筛选条件类,这个根据客户的查询要求来定义。Model类是数据类,是有Ling to SQL工具生成的类。查询得到的结果通过out引用参数返回。

 

转载于:https://www.cnblogs.com/caiwenz/p/3824364.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值