ASP:NET MVC搜索中的多个单词 [英] ASP:NET MVC multiple words in search
本文介绍了ASP:NET MVC搜索中的多个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当用户搜索时,我很难找出如何处理多个单词的方法.例如:搜索:"蓝色框",它应该能够找到:一个框是蓝色".我该怎么做?基本上这就是我的控制器看起来像atm的样子:
I having trouble finding out how to handle multiple words when the user searches. An example: Search: "Blue box" it should be able to find: "One box is blue". How would I do this? This is basically how my controller looks like atm:
public ActionResult Index(string searchString)
{
var posts = from s in _context.Posts
select s;
var postIndexViewModel = new PostIndexViewModel();
if (!String.IsNullOrEmpty(searchString))
{
posts = posts.Where(s => s.Title.Contains(searchString));
}
// More code here
return View(postIndexViewModel);
}
推荐答案
您的问题是您正在使用整个字符串进行包含.这意味着它必须按顺序包含蓝框"
.
Your problem is that you are doing the contains with a whole string. That means it must contain "Blue box"
in that order.
这是您需要做的:
var strings = searchString.Split(' ');
var finalPosts = new List<string>();
if (!String.IsNullOrEmpty(searchString))
{
foreach (var splitString in strings)
{
finalPosts.Add(posts.FirstOrDefault(s => s.Title.Contains(splitString)));
}
}
finalPosts
列表随后包含您的结果.
The finalPosts
list then contains your results.
这篇关于ASP:NET MVC搜索中的多个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文