动态构造Lambda表达式

环境:Visual Studio2008 .NET Framework3.5

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

动态构造代码:

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

 5      public   class  ConstructLambda < T >   where  T :  class new ()
 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 < string string >  dictionary  =   new  Dictionary < string string > () 
        
{
            
{"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);
            }

原创文章,转载请注明出处!
All CopyRight Reserved !

 

主页:http://jingtao.cnblogs.com

QQ:307073463
Email:[email protected]
MSN:[email protected]

你可能感兴趣的:(lambda)