我有搜索功能,可以将搜索字词传递给我的查询,并通过这些搜索字词过滤我的查询,但我不确定如何通过匹配的字词订购这些结果。
string[] seperator = { " " };
string[] filteredSearchTerms = searchTerm.Split(seperator, StringSplitOptions.None);
List<Ticket> tickets = (from t in entities.tbl_Ticket
where
t.tbl_Campaign.CampaignName == user.campaignName &
filteredSearchTerms.Any(v => t.Description.Contains(v))
select new Ticket
{
dateAdded = (DateTime)t.DateAdded,
description = t.Description,
name = t.Name,
ticketID = t.TicketID,
statusID = (int)t.StatusID,
SearchTerms = filteredSearchTerms.ToList()
}).AsEnumerable()
.Where(obj => filteredSearchTerms.Any(v => System.Text.RegularExpressions.Regex.IsMatch(obj.description, string.Format(@"\b{0}\b", v))))
.Take(10).ToList();
因此,如果第一个结果包含其描述中的所有单词,那么它应该出现在列表的顶部。我不确定在我目前的背景下是否可行。我已尝试使用类似Where扩展名的OrderByDescending作为过滤器。
任何帮助将不胜感激!
此致
TEZ
答案 0 :(得分:0)
您可以使用OrderByDescending()。以下为您的方案的示例。
string[] searchTerms = new[] { "one", "two", "three" };
var descriptions = new List<string>(new string[] { "one string", "two string", "one two three string", "one two string", "no match" });
var filteredDescriptions = descriptions.Where(description => searchTerms.Count(searchTerm => description.Contains(searchTerm)) > 0);
var orderedDescriptions = filteredDescriptions.OrderByDescending(description => searchTerms.Count(searchTerm => description.Contains(searchTerm)));
我得到以下结果。
一二三串 一两串 一弦 两个字符串
希望这有帮助。