OrderBy 与来自搜索功能的匹配词

OrderBy with matched words from a search feature

我有将搜索词传递给我的查询的搜索功能,我通过这些搜索词过滤我的查询,但我非常不确定如何通过匹配的词对这些结果进行排序。

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();

因此,如果第一个结果包含其描述中的所有单词,那么它应该出现在列表的顶部。我不确定在我目前的情况下是否可行。我试过 OrderByDescending 与过滤器类似的 Where 扩展。

任何帮助将不胜感激!

此致,

特兹

您可以使用 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)));

我得到以下结果。

一二三串 一两串 一弦 两个字符串

希望对您有所帮助。