构建动态Lambda表达式

时间:2018-09-17 12:26:09

标签: dynamic lambda tree expression expression-trees

我知道如何构建像 x => x> 5 的简单lambda:

int[] nbs = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };            
IEnumerable<int> result1 = nbs.Where(x => x > 5);

ParameterExpression parameter = Expression.Parameter(typeof(int), "x");
ConstantExpression constant = Expression.Constant(5);
BinaryExpression expressionBody = Expression.GreaterThan(parameter, constant);
Expression<Func<int, bool>> expression = Expression.Lambda<Func<int, bool>>(expressionBody, parameter);
IEnumerable<int> result2 = nbs.Where(expression.Compile());

但是,我该如何构建一个像这样的lambda p => p.Cars.Any(c => c.Horsepowers> 300)

public class Person
{
    public string Name { get; set; }
    public List<Car> Cars { get; set; }
}

public class Car
{
    public string Make { get; set; }
    public int Horsepowers { get; set; }
}

Person p1 = new Person();
p1.Name = "Thom";
p1.Cars = new List<Car>()
{
 new Car(){Horsepowers = 100, Make = "Toyota"},
 new Car(){Horsepowers = 200, Make = "Fiat"},
 new Car(){Horsepowers = 300, Make = "Audi"},
 new Car(){Horsepowers = 400, Make = "Ferrari"}
};

Person p2 = new Person();
p2.Name = "Allen";
p2.Cars = new List<Car>()
{
 new Car(){Horsepowers = 500, Make = "McLaren"},
 new Car(){Horsepowers = 200, Make = "Volvo"},
 new Car(){Horsepowers = 300, Make = "Audi"},
 new Car(){Horsepowers = 400, Make = "Ferrari"}
};

List<Person> persons = new List<Person>();
persons.Add(p1);
persons.Add(p2);

IEnumerable<Person> res = persons.Where(p => p.Cars.Any(c => c.Horsepowers > 300));

换句话说,我该如何动态构建一个表达式(Expression<Func<Person, bool>>),以便可以将其作为参数传递给Where方法?

1 个答案:

答案 0 :(得分:0)

要创建复杂的表达式树,请从叶子开始,然后向上直到根。就您而言,您可以从构建内部Lambda c => c.Horsepowers > 300开始:

var c = Expression.Parameter(typeof(Car), "c");
var horsepower = Expression.PropertyOrField(c, "Horsepowers");
var minHorsepower = Expression.Constant(300);
var grateThen = Expression.GreaterThan(horsepower, minHorsepower);
var innerLambda = Expression.Lambda<Func<Car, bool>>(grateThen, c);

现在,您可以创建p.Cars.Any(...)并将内部lambda放入其中

var p = Expression.Parameter(typeof(Person), "p");
var cars = Expression.PropertyOrField(p, "Cars");
var any = Expression.Call(
    typeof(Enumerable), 
    nameof(Enumerable.Any), 
    new[] {typeof(Car)}, 
    cars,
    innerLambda
  );

之后,构建您的根lambda:

var lambda = Expression.Lambda<Func<Person, bool>>(any, p);