动态构造Lambda表达式

环境:Visual Studio2008 .NET Framework3.5

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

动态构造代码:

 1 ExpandedBlockStart.gif ContractedBlock.gif      /**/ /// <summary>
 2InBlock.gif    /// 动态构造Lambda表达式
 3InBlock.gif    /// </summary>
 4ExpandedBlockEnd.gif    /// <typeparam name="T">查询目标实体</typeparam>

 5 None.gif      public   class  ConstructLambda < T >   where  T :  class new ()
 6 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
 7InBlock.gif        private Type TType;
 8ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 9InBlock.gif        /// 构造方法
10ExpandedSubBlockEnd.gif        /// </summary>

11InBlock.gif        public ConstructLambda()
12ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
13InBlock.gif            TType = typeof(T);
14ExpandedSubBlockEnd.gif        }

15ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
16InBlock.gif        /// 构造与表达式
17InBlock.gif        /// </summary>
18InBlock.gif        /// <param name="dictionary">构造源</param>
19ExpandedSubBlockEnd.gif        /// <returns>lambda表达式</returns>

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

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

32InBlock.gif
33ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
34InBlock.gif        /// 构造或表达式
35InBlock.gif        /// </summary>
36InBlock.gif        /// <param name="dictionary">构造源</param>
37ExpandedSubBlockEnd.gif        /// <returns>Lambda表达式</returns>

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

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

50 None.gif


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

None.gif      public   class  Person
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public string Name dot.gifgetset; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public string Sex dot.gifgetset; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public int Age dot.gifgetset; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public DateTime Birthday dot.gifgetset; }
InBlock.gif
InBlock.gif        
public Person()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{ }
ExpandedBlockEnd.gif    }

None.gif

      虚拟查找源:

None.gif          private  Dictionary < string string >  dictionary  =   new  Dictionary < string string > () 
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{"Name","JT"},dot.gif{"Sex",""},dot.gif{"Age","20"},dot.gif{"Birthday","02/02/2008"}
ExpandedBlockEnd.gif        }
;
None.gif


 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"))

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

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

 

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

QQ:307073463
Email:jingtaodeemail@qq.com
MSN:sunjingtao@live.com

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值