zoukankan      html  css  js  c++  java
  • 组合查询功能实现

    前言

      这是我的第二篇文章,这是我之前做的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引用参数返回。

  • 相关阅读:
    最长公共上升子序列
    最长公共子序列
    3847: Mowing the Lawn (单调队列)
    A/B(扩展欧几里得)
    One Person Game(扩展欧几里得)
    Substring with Concatenation of All Words, 返回字符串中包含字符串数组所有字符串元素连接而成的字串的位置
    Divide two numbers,两数相除求商,不能用乘法,除法,取模运算
    Merge k Sorted Lists, k路归并
    二路归并排序,利用递归,时间复杂度o(nlgn)
    StrStr,判断一个字符串是不是另一个字符串的字串,并返回子串的位置
  • 原文地址:https://www.cnblogs.com/caiwenz/p/3824364.html
Copyright © 2011-2022 走看看