我一直在谷歌搜索,但没有找到任何有关在ASP.NET MVC项目中构建良好搜索功能的好信息。
在我的脑海中,搜索作者和书籍的搜索功能如下所示:
char[] delimiterChars = { ' ', ','};
string[] searchterms = SearchBox.Split(delimiterChars);
IQueryable<SearchResult> SR = _db.Books.Where(e =>
(e.Title.Contains(SearchTerm[0]) || e.Author.Name.Contains(SearchTerm[0]))
&& (e.Title.Contains(SearchTerm[1]) || e.Author.Name.Contains(SearchTerm[1]))
//&& repeat for as many words were entered in the search box
)
.Select(e => new SearchResult
{
Title = e.Title,
Type = "Book",
Link = "Book/" + e.BookID
});
SR
变量吗?在上面的搜索中,我正在搜索与图书或作者匹配的结果。也许在我想要在Author表中查找匹配项之后立即执行搜索,然后将这些结果附加到第一个查询的SR
变量结果中。我不知道这是否是最实用的方法,但可以做到吗?
答案 0 :(得分:0)
我的第一个倾向是将其卸载到像Lucene.Net这样的东西上。没错,你可以这样做:
char[] delimiterChars = { ' ', ','};
string[] searchterms = SearchBox.Split(delimiterChars);
var temp = _db.Books.AsQueryable();
foreach (string term in searchterms)
{
temp = temp.Where(e =>
(e.Title.Contains(term) || e.Author.Name.Contains(term));
}
IQueryable<SearchResult> results = temp.Select(e => new SearchResult
{
Title = e.Title,
Type = "Book",
Link = "Book/" + e.BookID
});
请注意动态构建查询的foreach。