我们应用程序中的多个不同查询都包含了一组特定的条件。为避免代码重复,我想将这些条件拆分为一个方法,并返回一个表达式,以便随时应用到需要的地方。
然而,下面的语句无法编译,因为只是一个。
有没有可能像这样使用表达式?
public Expression<Func<Invoice, bool>> GetComplexPredicate()
{
// complex predicate is returned as an Expression:
return c => ...
}
直接重用:
var result = repository.Invoice.Where(GetComplexPredicate())
然而,下面的语句无法编译,因为只是一个。
var result = repository.Customer
.Where(c => c.Country == "US" && c.Invoice.Any(GetComplexPredicate()))
有没有可能像这样使用表达式?