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

前端 未结 1 554
北海茫月
北海茫月 2020-12-08 15:30

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

相关标签:
1条回答
  • 2020-12-08 16:23
    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 = ...

    0 讨论(0)
提交回复
热议问题