我目前正在建立一个论坛,允许用户提问并回答问题。 为了让用户更容易查找某个问题,我尝试构建搜索引擎。 代码:
List<QuestionDto> dto = _db.Questions.Where(x => (x.Title.Contains(temp) || x.Message.Contains(temp) || (x.User.FirstName + " " + x.User.LastName).Contains(temp) || x.User.LastName.Contains(temp)) && (categoryId == null || x.Category.CategoryId == categoryId))
.Select(question => new QuestionDto { QuestionId = question.QuestionId,
Votes = question.Votes, Title = question.Title,
Message = question.Message, CategoryName = question.Category.Name,
EditDate = question.EditDate, DateOfCreation = question.DateOfCreation,
User = new UserDto { UserId = question.User.Id, FirstName = question.User.FirstName, LastName = question.User.LastName, ImageFile = question.User.ImageFile },
Tags = question.Tags.Select(x => x.Tag).ToList()
}).ToList();
字符串temp用于搜索问题,以查看标题,消息或用户名是否与查询匹配。
我现在想要完成的是根据问题中找到的点击量对结果进行排序。因此,例如,如果用户搜索单词&#34; PHP&#34;结果将是:
问题1:PHP被发现10次 问题2:PHP被发现5次 等等。
如何接近这个?
答案 0 :(得分:2)
我假设你想要在db with Contains()中搜索的属性列表中找到临时词的出现位置。
这是一个循序渐进的过程,它将为您提供一个包含问题ID和出现次数的字典。您可以使用此词典订购问题列表。
当然可以优化它。
如果您需要订购问题,可以使用字典并订购。如果您只需要显示出现次数,您将拥有以下列方式为您提供数据的字典:问题#QuestionId,搜索字符串的出现次数。
假设单词是PHP,您将拥有:,等等。记录不会在字典中订购。
我为该词典添加了一个订单步骤。
稍后编辑:如果您尝试在您的方法中使用Regex,请注意您不能在select语句中使用Regex,但是在将所有记录带入内存后(.ToList()
之后)可以使用Regex 。请使用以下问题作为参考:Using "Match" in a Linq statement
List<QuestionDto> dtoQList = _db.Questions.Where(x => (x.Title.Contains(temp) || x.Message.Contains(temp) || (x.User.FirstName + " " + x.User.LastName).Contains(temp) || x.User.LastName.Contains(temp)) && (categoryId == null || x.Category.CategoryId == categoryId))
.Select(question => new QuestionDto
{
QuestionId = question.QuestionId,
Votes = question.Votes,
Title = question.Title,
Message = question.Message,
CategoryName = question.Category.Name,
EditDate = question.EditDate,
DateOfCreation = question.DateOfCreation,
User = new UserDto { UserId = question.User.Id, FirstName = question.User.FirstName, LastName = question.User.LastName, ImageFile = question.User.ImageFile },
Tags = question.Tags.Select(x => x.Tag).ToList()
}).ToList();
var questionDictionaryMatchings = new Dictionary<int, int>();
foreach(var question in dtoQList)
{
var titleSplited = question.Title.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries);
var messageSplited = question.Message.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries);
var firstNameSplited = question.User.FirstName.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries);
var lastNameSplited = question.User.LastName.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries);
var titleSplitedQuery = from word in titleSplited
where word.ToLowerInvariant() == temp.ToLowerInvariant()
select word;
int wordCountTitle = titleSplitedQuery.Count();
var messageSplitedQuery = from word in messageSplited
where word.ToLowerInvariant() == temp.ToLowerInvariant()
select word;
int wordCountMessage = messageSplitedQuery.Count();
var firstNameSplitedQuery = from word in firstNameSplited
where word.ToLowerInvariant() == temp.ToLowerInvariant()
select word;
int wordCountFirstName = firstNameSplitedQuery.Count();
var lastNameSplitedQuery = from word in lastNameSplited
where word.ToLowerInvariant() == temp.ToLowerInvariant()
select word;
int wordCountLastName = lastNameSplitedQuery.Count();
questionDictionaryMatchings.Add(question.QuestionId, wordCountTitle + wordCountMessage + wordCountFirstName + wordCountLastName);
}
//sorted dictionary
var questionDictionaryMatchingsSorted = from entry in questionDictionaryMatchings orderby entry.Value descending select entry;