-->

Dynamic linq expression tree with nested propertie

2019-05-11 05:25发布

问题:

I have a list which I must filter on child properties. The filter operator is dynamic and I'm using a predicate builder in order to combine several filters/lambdas.

For simplicity, let's say that I have two classes like this:

public class FirstClass
{
    public int Id { get; set; }
    public ICollection<SecondClass> MyList { get; set; }
}

public class SecondClass
{
    public int ReferenceId { get; set; }
    public int Value { get; set; }
}

My filter use a reference id, an operator type and a value, such that a pseudo code would be like this:

"list of FirstClass".Where(param1 => 
    param1.MyList.Single(param2 => 
        param2.ReferenceId == "reference id").Value "operatorType" "value")

The actual filter will be something like 123 eq 456, where the reference id is 123, operatorType is "eq" and value is 456.

If the operator just was equality, then the following works just fine:

Expression<Func<FirstClass, bool>> lambda = 
    param1 => param1.MyList.Single(param2 => param2.ReferenceId == id).Value == value;

Also, filtering only on FirstClass with dynamic expressions, works like a charm, e.g. filtering on Id (my ExpressionTypeDictionary is a dictionary for selecting an ExpressionType based on the provided operatorType):

var parameter = Expression.Parameter(typeof(FirstClass), "param1");
Expression body = parameter;
body = Expression.Property(body, "Id");
body = Expression.MakeBinary(ExpressionTypeDictionary[operatorType], body, value);
var lambda = Expression.Lambda<Func<FirstClass, bool>>(body, new[] { parameter });

I'm able to get the following to compile, but executing the filter on real data using EF Core returns an exception for querySource:

var parameter = Expression.Parameter(typeof(FirstClass), "param1");
Expression<Func<FirstClass, int>> left = param1 => 
    param1.MyClass.Single(param2 => param2.ReferenceId == id).Value;
var body = Expression.MakeBinary(
    ExpressionTypeDictionary[operatorType], 
    left.Body, 
    Expression.Constant(value));
var lambda = Expression.Lambda<Func<FirstClass, bool>>(body, new[] { parameter });
...
theList.Where(lambda);

Any suggestions are appreciated :)

回答1:

I think rather than expression like this

Expression<Func<FirstClass, bool>> predicate = 
    x => x.MyList.Single(y => y.ReferenceId == id).Value [operator] value;

you'd better build an expression like this:

Expression<Func<FirstClass, bool>> predicate = 
    x => x.MyList.Any(y => y.ReferenceId == id && y.Value == value);

Here is how you can do that:

var innerParameter = Expression.Parameter(typeof(SecondClass), "y");
var innerPredicate = Expression.Lambda<Func<SecondClass, bool>>(
    Expression.AndAlso(
        Expression.Equal(Expression.Property(innerParameter, "ReferenceId"), Expression.Constant(id)),
        Expression.MakeBinary(ExpressionTypeDictionary[operatorType], Expression.Property(innerParameter, "Value"), Expression.Constant(value))),
    innerParameter);
var parameter = Expression.Parameter(typeof(FirstClass), "x");
var predicate = Expression.Lambda<Func<FirstClass, bool>>(
    Expression.Call(
        typeof(Enumerable), "Any", new Type[] { typeof(SecondClass) },
        Expression.Property(parameter, "MyList"), innerPredicate),
    parameter);