使用。载的条款一领域内的搜索功能 [英] Search Feature using .Contains with terms within a one field
问题描述
我已为产品网站建立了搜寻功能。搜索工作正常。最近,我们在数据库中添加了一个SearchTerm字段。
SearchTerm数据示例:work shoes blue black gear
当前代码是
I have built a search feature for a product site. The search works fine. Recently, we have added a SearchTerm field in the database. SearchTerm Data example: "work shoes blue black gear" Current code is
pM = (from p in ctx.Products
where
p.productSearchField.Contains(term) ||
p.productName.Contains(term)
select p).ToList()
如果术语=shoes< - works
If the term = "shoes" <-- works
如果术语=工作鞋< - 工作
If the term = "work shoes" <-- works
如果术语=黑鞋< - 不起作用。
If the term = "black shoes" <-- does NOT work.
我也尝试用逗号分隔SearchTerm数据,但是没有用。
任何建议?
I also tried to separate the SearchTerm Data by commas, but that did not work. Any advice?
推荐答案
将术语拆分为单个术语。查看是否有任何匹配项:
Split the term into single terms. See if any match:
term.Split(' ').Any(i => p.productSearchField.Contains(i))
或者如果每个词都必须匹配:
Or if every word has to match:
term.Split(' ').All(i => p.productSearchField.Contains(i))
EDIT
EDIT
var terms = term.Split(' ');
pM = (from p in ctx.Products
where
terms.All(i => p.productSearchField.Contains(i)) ||
p.productName.Contains(term)
select p).ToList()
这篇关于使用。载的条款一领域内的搜索功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!