如何在Linq到EF中使用Expression <Func >的条件?

关于这个主题已经存在一些问题(例如entity framework中的Expression.Invoke? ),但是,我无法找到适合我具体情况的答案。 我想定义一个这样的方法:

public IQueryable GetCustomers(Expression<Func> 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”。 我究竟做错了什么?

如果要使用表达式树,则需要将表达式树本身传递给LINQ方法:

 return ctx.Customers.AsExpandable().Where(condition)