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(); 表达式请问怎么写啊!请帮帮忙!一个新手的求救!
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();