When invoking a method-provided expression on a member property, LinqKit throws an InvalidCastException.

c# entity-framework expression-trees linq linqkit


Given a simple parent/child class structure. I want to use linqkit to apply a child lambda expression on the parent. I also want the Lambda expression to be provided by a utility method.

public class Foo
    public Bar Bar { get; set; }

public class Bar
    public string Value { get; set; }
    public static Expression<Func<Bar, bool>> GetLambdaX()
        return c => c.Value == "A";

Expression<Func<Foo, bool>> lx = c => Bar.GetLambdaX().Invoke(c.Bar);

The above code throws

System.InvalidCastException: Unable to cast object of type 
'System.Linq.Expressions.MethodCallExpression' to type 
at LinqKit.ExpressionExpander.VisitMethodCall(MethodCallExpression m)
at LinqKit.ExpressionVisitor.Visit(Expression exp)
at LinqKit.ExpressionVisitor.VisitLambda(LambdaExpression lambda)
at LinqKit.ExpressionVisitor.Visit(Expression exp)
at LinqKit.Extensions.Expand<TDelegate>(Expression`1 expr)
2/2/2015 9:08:17 PM

Accepted Answer

var lambdaX = Bar.GetLambdaX();

Expression<Func<Foo, bool>> lx = c => lambdaX.Invoke(c.Bar);

This works.

Be careful with writing

Expression<Func<Foo, bool>> lx = ...

Before assignment the compiler processes this line and in your case creates a Method Call Expression (i.e. for calling Bar.GetLambdaX()) that is assigned to lx.

If you use var ... then Bar.GetLambdaX(); is called and assigned, the value of which (which is the lambda expression) is used later in lx = ...

2/22/2011 12:06:59 PM

Related Questions

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow