Tag: 构建器

LINQ to Entities查询中可重用的谓词表达式

在我们的应用程序中,许多不同查询中出现的一组标准已经慢慢变得更加复杂。 为了避免重复此代码,我想将这些条件拆分为一个方法,该方法将条件作为表达式返回,然后可以在必要时应用它: public Expression<Func> GetComplexPredicate() { // complex predicate is returned as an Expression: return c => … } 重复使用: var result = repository.Invoice.Where(GetComplexPredicate()) 但是,下面的语句不会编译,因为c.Invoice只是一个ICollection 。 var result = repository.Customer .Where(c => c.Country == “US” && c.Invoice.Any(GetComplexPredicate())) 是否可以使用这样的表达式?