我尝试重构以下代码(按类别和搜索词过滤一些数据库记录):
from entry in _DB.TheTable
join otherEntry in _DB.AnotherTable
on entry.PrimaryKey equals otherEntry.Field
where category.Any(String.IsNullOrWhiteSpace)
|| category.Contains(entry.ProductGroup)
where String.IsNullOrWhiteSpace(searchTerm)
|| entry.ProductNumber.Contains(searchTerm)
|| otherEntry.Name1.Contains(searchTerm)
|| otherEntry.Name2.Contains(searchTerm)
|| otherEntry.Name3.Contains(searchTerm)
select new {entry.Something, entry.SomethingElse};
看起来像这样:
from entry in _DB.TheTable
join otherEntry in _DB.AnotherTable on entry.PrimaryKey equals otherEntry.Field
let searchFields = new[]{entry.ProductNumber, otherEntry.Name1, otherEntry.Name2,/*...*/}
where category.Any(String.IsNullOrWhiteSpace)
|| category.Contains(entry.ProductGroup)
where String.IsNullOrWhiteSpace(searchTerm)
|| searchFields.Any(field=>field.Contains(searchTerm))
select new {entry.Something, entry.SomethingElse};
不幸的是,LINQ to SQL 似乎无法做到这一点:我ArgumentException
从代码中得到一个searchFields.Any(field=>field.Contains(searchTerm))
说明
参数
value
类型错误。预期:System.String.
实际:System.String[].
我该如何调整它才能工作?
编辑:对不起,忘了说searchTerm
是一个string
,category
是一个string[]
。