首页 > 其他 > 详细

Linq语句 动态组建

时间:2016-01-06 23:29:39      阅读:149      评论:0      收藏:0      [点我收藏+]
 1        public override System.Linq.Expressions.Expression<Func<I_Position_Access, bool>> GetWhereLambda(I_Position_Access t)
 2         {
 3             ParameterExpression parameter = Expression.Parameter(typeof(I_Position_Access), "b");//创建参数b
 4             System.Linq.Expressions.ConstantExpression constant = Expression.Constant(t.I_Position_AccessID);//创建常数 
 5             MemberExpression member = Expression.PropertyOrField(parameter, "I_Position_AccessID");
 6 
 7             System.Linq.Expressions.BinaryExpression bin =
 8                 Expression.GreaterThan(member, constant);//创建 i > 常数 
 9 
10             if (t.I_Position_AccessID > 0)
11             {
12                 constant = Expression.Constant(t.I_Position_AccessID);
13                 member = Expression.PropertyOrField(parameter, "I_Position_AccessID");
14                 bin = Expression.Equal(member, constant);
15             }
16             if (t.I_PositionID > 0)
17             {
18                 constant = Expression.Constant(Convert.ToInt32(t.I_PositionID), typeof(int?));
19                 member = Expression.PropertyOrField(parameter, "I_PositionID");
20                 System.Linq.Expressions.BinaryExpression where2 = Expression.Equal(member, constant);
21 
22                 bin = Expression.And(bin, where2);
23             }
24             if (t.I_AccessID > 0)
25             {
26                 constant = Expression.Constant(Convert.ToInt32(t.I_AccessID), typeof(int?));
27                 member = Expression.PropertyOrField(parameter, "I_AccessID");
28                 System.Linq.Expressions.BinaryExpression whereI_AccessID = Expression.Equal(member, constant);
29                 bin = Expression.And(bin, whereI_AccessID);
30             }
31             if (t.OpenState > 0)
32             {
33                 constant = Expression.Constant(Convert.ToInt32(t.OpenState), typeof(int?));
34                 member = Expression.PropertyOrField(parameter, "OpenState");
35                 System.Linq.Expressions.BinaryExpression whereOpenState = Expression.Equal(member, constant);
36                 bin = Expression.And(bin, whereOpenState);
37             }
38             return Expression.Lambda<Func<I_Position_Access, bool>>(bin, parameter);
39             //获取Lambda表达式
40         }

 

Linq语句 动态组建

原文:http://www.cnblogs.com/demoC/p/5107520.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!