LINQ to SQL 查询帮助(字符串包含字符串数组中的任何字符串)
我一直在用这个撕掉我的头发。我有一个搜索词数组,我正在尝试执行 LINQ to SQL 查询来针对数组中的每个项目搜索字段值。
我已经走到这一步了
var searchResults =
from x in SDC.Staff_Persons
where staffTermArray.Any(pinq => x.Forename.Contains(pinq))
|| staffTermArray.Any(pinq => x.Surname.Contains(pinq))
|| staffTermArray.Any(pinq => x.Known_as.Contains(pinq))
orderby x.Surname
select x;
......但后来得到了
本地序列不能在 LINQ 中使用 到SQL执行查询 除 Contains() 之外的运算符 运算符
...现在我被困住了。
如果有人能提供帮助,我将非常感激。 提前致谢。
抢
I've been tearing my hair out with this one. I've got an array of search terms and I'm trying to do a LINQ to SQL query to search field values against each item in the array.
I got this far..
var searchResults =
from x in SDC.Staff_Persons
where staffTermArray.Any(pinq => x.Forename.Contains(pinq))
|| staffTermArray.Any(pinq => x.Surname.Contains(pinq))
|| staffTermArray.Any(pinq => x.Known_as.Contains(pinq))
orderby x.Surname
select x;
... but then got
Local sequence cannot be used in LINQ
to SQL implementation of query
operators except the Contains()
operator
... and now I'm stuck.
If anyone can help I'd be very grateful.
Thanks in advance.
Rob
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定这是否是最简单的解决方案,但这会起作用:
您将需要 PredicateBuilder 为此。
I'm not sure if this is the easiest solution, but this will work:
You will need the PredicateBuilder for this to work.
一种选择是在客户端而不是在 SQL 中进行过滤。您可以通过调用
AsEnumerable()
强制在客户端上计算where
。但是,这意味着表的每一行都会在测试匹配之前加载到内存中,因此如果您的搜索仅匹配大型表中的少量结果,则效率可能会低得令人无法接受。One option would be to do the filtering on the client rather than in SQL. You can force the
where
to be evaluated on the client by callingAsEnumerable()
. However, this means that every row of the table is loaded into memory before being tested for a match, so it may be unacceptably inefficient if your search matches only a small number of results from a large table.