首页 新闻 会员 周边 捐助

动态构建Lambda 的问题,请指教,急!

0
悬赏园豆:200 [已解决问题] 解决于 2011-03-24 22:55
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public int Salary { get; set; }
}
List
<Person> Persons=new List<Person> ()
Type TType
=typeof(Person);

ParameterExpression pp
= Expression.Parameter(TType, "p");

Expression temp
= Expression.GreaterThan(Expression.Call(Expression.Property(pp, TType.GetProperty("Age")), ??????),
Expression.Constant(
20));
var lambda
= Expression.Lambda<Func<Person, bool>>(temp, pp);

我想动态构建出Persons.Where(p=>p.Age>20).ToList(); 表达式请问怎么写啊!请帮帮忙!一个新手的求救!

呗嘞呜的主页 呗嘞呜 | 初学一级 | 园豆:2
提问于:2011-03-22 20:55
< >
分享
最佳答案
0
var people = new List<Person> {
new Person{ Name = "鹤冲天", Age = 40},
new Person{ Name = "鹤中天", Age = 18},
};
//动态构建表达式
var param = Expression.Parameter(typeof(Person), "p");
var body
= Expression.GreaterThan(
Expression.Property(param,
"Age"),
Expression.Constant(
20)
);
var expression
= Expression.Lambda<Func<Person, bool>>(body, param);
//
var ps = people.Where(expression.Compile()).ToList();
收获园豆:200
鹤冲天 | 老鸟四级 |园豆:2379 | 2011-03-22 22:02
感谢鹤冲天
回答问题解决!
呗嘞呜 | 园豆:2 (初学一级) | 2011-03-24 22:55
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册