0

I know how to build a simple lambda like x => x > 5:

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());

But, how do I build a lambda like this 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));

In other words, how do I build an expression ( Expression<Func<Person, bool>> ) dynamically that I can pass as a parameter to the Where method?

1 Answer 1

0

To create a complex expression tree start from leaves and go up to the root. In your case you can start with building inner 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);

Now you can create p.Cars.Any(...) and put your inner lambda inside

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

After that build your root lambda:

var lambda = Expression.Lambda<Func<Person, bool>>(any, p);
2
  • Ok, I didn't realize that I had to pass cars as an argument as well to the Expression.Call-method! Feels a little bit strange that cars also should be an argument to the Any-method...
    – Andy Rehn
    Sep 17, 2018 at 14:45
  • @AndyRehn it's because Any is an extension method. You can write Enumerable.Any(cars, c => ...) or just cars.Any(c => ...) Sep 17, 2018 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.