Consider this LINQ To SQL query. It's intention is to take a string[] of search terms and apply the terms to a bunch of different fields on the SQL table:
```string[] searchTerms = new string[] {"hello","world","foo"};
List<Cust> = db.Custs.Where(c =>
searchTerms.Any(st => st.Equals(c.Email))
|| searchTerms.Any(st => st.Equals(c.FirstName))
|| searchTerms.Any(st => st.Equals(c.LastName))
|| searchTerms.Any(st => st.Equals(c.City))
|| searchTerms.Any(st => st.Equals(c.Postal))
|| searchTerms.Any(st => st.Equals(c.Phone))
|| searchTerms.Any(st => c.AddressLine1.Contains(st))
)
.ToList();
出现了一个异常:
除了 Contains() 操作符之外,本地序列不能用于 LINQ to SQL 查询运算符的实现
问题: 为什么会出现此异常,如何重写查询以避免此异常?
PredicateBuilder
的强大功能可能会让人失望,因为一些条件可能无法被 LinqToSql 提供程序实现(这正是你在问题中描述的确切问题)。 - Matt Kocaj.Contains()
- 通常是 LinqToSql 不支持的等式的良好替代品。建议小心使用PredicateBuilder
。 - Matt Kocaj