zoukankan      html  css  js  c++  java
  • 动态构造Lambda表达式

    场合:查询条件不确定,需动态生成

    动态构造代码:
     1    /// <summary>
     2    /// 动态构造Lambda表达式
     3    /// </summary>
     4    /// <typeparam name="T">查询目标实体</typeparam>

     5    public class ConstructLambda<T> where T : classnew()
     6    {
     7        private Type TType;
     8        /// <summary>
     9        /// 构造方法
    10        /// </summary>

    11        public ConstructLambda()
    12        {
    13            TType = typeof(T);
    14        }

    15        /// <summary>
    16        /// 构造与表达式
    17        /// </summary>
    18        /// <param name="dictionary">构造源</param>
    19        /// <returns>lambda表达式</returns>

    20        public Expression<Func<T, bool>> GetAndLambdaExpression(Dictionary<stringstring> dictionary)
    21        {
    22            Expression expression_return = Expression.Constant(true);
    23            ParameterExpression expression_param = Expression.Parameter(TType, "p");
    24            foreach (string key in dictionary.Keys)
    25            {
    26                Expression temp = Expression.Equal(Expression.Call(Expression.Property(expression_param, TType.GetProperty(key)), TType.GetMethod("ToString")),
    27                    Expression.Constant(dictionary[key]));
    28                expression_return = Expression.And(expression_return, temp);
    29            }

    30            return (Expression<Func<T, bool>>)Expression.Lambda<Func<T, bool>>(expression_return, new ParameterExpression[] { expression_param });
    31        }

    32
    33        /// <summary>
    34        /// 构造或表达式
    35        /// </summary>
    36        /// <param name="dictionary">构造源</param>
    37        /// <returns>Lambda表达式</returns>

    38        public Expression<Func<T, bool>> GetOrLambdaExpression(Dictionary<stringstring> dictionary)
    39        {
    40            Expression expression_return = Expression.Constant(false);
    41            ParameterExpression expression_param = Expression.Parameter(TType, "p");
    42            foreach (string key in dictionary.Keys)
    43            {
    44                Expression temp = Expression.Equal(Expression.Call(Expression.Property(expression_param, TType.GetProperty(key)), TType.GetMethod("ToString")),
    45                    Expression.Constant(dictionary[key]));
    46                expression_return = Expression.Or(expression_return, temp);
    47            }

    48            return (Expression<Func<T, bool>>)Expression.Lambda<Func<T, bool>>(expression_return, new ParameterExpression[] { expression_param });
    49        }

    50


    实例:
    测试数据:
            虚拟实体:

        public class Person
        
    {
            
    public string Name getset; }
            
    public string Sex getset; }
            
    public int Age getset; }
            
    public DateTime Birthday getset; }

            
    public Person()
            
    { }
        }

          虚拟查找源:

            private Dictionary<stringstring> dictionary = new Dictionary<stringstring>() 
            
    {
                
    {"Name","JT"},{"Sex",""},{"Age","20"},{"Birthday","02/02/2008"}
            }
    ;


     1,无条件查找:    
    new ConstructLambda<Person>().GetAndLambdaExpression(new Dictionary<string, string>()).ToString()返回结果:
    p => True

      new ConstructLambda<Person>().GetOrLambdaExpression(new Dictionary<string, string>()).ToString()返回结果:
    p => False
     2,多条件查找:
      new ConstructLambda<Person>().GetAndLambdaExpression(dictionary).ToString()返回结果:
    p => ((((True And (p.Name.ToString() = "JT")) And (p.Sex.ToString() = "男")) And (p.Age.ToString() = "20")) And (p.Birthday.ToString() = "02/02/2008"))

      new ConstructLambda<Person>().GetOrLambdaExpression(dictionary).ToString()返回结果:
    p => ((((False Or (p.Name.ToString() = "JT")) Or (p.Sex.ToString() = "男")) Or (p.Age.ToString() = "20")) Or (p.Birthday.ToString() = "02/02/2008"))

               //构造常量表达式
                Expression expression_return = Expression.Constant(true);
                
    //构造表达式参数  类似于p=> 中的p
                ParameterExpression expression_param = Expression.Parameter(TType, "p");
                
    //遍历所有关键词 生成查询条件
                foreach (string key in dictionary.Keys)
                
    {
                    
    //生成类似与“p.Name.ToString()==常量”的表达式
                    Expression temp = Expression.Equal(
                        
    //后面两行先用.Property 得到p.Name 然后用call得到p.Name.ToString();
                        Expression.Call(
                        Expression.Property(expression_param, TType.GetProperty(key)),TType.GetMethod(
    "ToString")),
                        Expression.Constant(dictionary[key]));
                    
    //合并查询条件
                    expression_return = Expression.And(expression_return, temp);
                }

  • 相关阅读:
    分布式任务调度 xxl-job
    【线上】 select * from xxoo where 1=1应用挂掉
    【死磕ES】七、基础检索
    【死磕ES】四、基本操作
    【死磕ES】三、基本概念
    【死磕ES】二、搭建环境
    Mac共享文件夹
    微信小程序下拉刷新,上拉加载
    微信小程序textarea输入框出现[object Object]
    微信小程序official-account的使用
  • 原文地址:https://www.cnblogs.com/hyl8218/p/2184051.html
Copyright © 2011-2022 走看看