There have already been some questions about this topic (for instance Expression.Invoke in Entity Framework?), however, I could not find an answer for my specific situation. I would like to define a method like this:
public IQueryable<Customer> GetCustomers(Expression<Func<Customer, bool>> condition)
{
return from p in ctx.Customers.AsExpandable()
where condition.Compile()(p)
select p;
}
The AsExpandable method is from LinqKit (as it was adviced in the thread mentioned before). However, when I try to call my method like his:
var customers = GetCustomers(c => c.ID == 1);
It throws an InvalidCastException:
Unable to cast object of type 'System.Linq.Expressions.InstanceMethodCallExpressionN' to type 'System.Linq.Expressions.LambdaExpression'. What am I doing wrong?
If you want to use an expression tree, you need to pass the expression tree itself to the LINQ method:
return ctx.Customers.AsExpandable().Where(condition)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With