このトピックについてはすでにいくつかの質問がありますが(たとえば、Expression.Invoke in Entity Framework?)、特定の状況に対する答えを見つけることができませんでした。次のようなメソッドを定義したいと思います。
public IQueryable<Customer> GetCustomers(Expression<Func<Customer, bool>> condition)
{
return from p in ctx.Customers.AsExpandable()
where condition.Compile()(p)
select p;
}
AsExpandableメソッドはLinqKitからのものです(前述のスレッドでアドバイスされたように)。ただし、彼のようにメソッドを呼び出そうとすると、次のようになります。
var customers = GetCustomers(c => c.ID == 1);
それはInvalidCastExceptionをスローします:
タイプ'System.Linq.Expressions.InstanceMethodCallExpressionN'のオブジェクトをタイプ'System.Linq.Expressions.LambdaExpression'にキャストできません。私は何が間違っているのですか?