LinqKit System.InvalidCastException When Invoking method-provided expression on member property.

Posted by mdworkin on Stack Overflow See other posts from Stack Overflow or by mdworkin
Published on 2010-04-02T04:13:04Z Indexed on 2010/04/02 4:23 UTC
Read the original article Hit count: 439

Filed under:
|
|

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);
Console.WriteLine(lx.Expand());

The above code throws

System.InvalidCastException: Unable to cast object of type 
'System.Linq.Expressions.MethodCallExpression' to type 
'System.Linq.Expressions.LambdaExpression'.
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)
....

Please help!

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ