1

我已将数据存储在列表中,例如

 List<SearchResult> list = new List<SearchResult>();
 SearchResult sr = new SearchResult();
 sr.Description = "sample description";
 list.Add(sr);

假设我的数据存储在描述字段中,例如

"JCB Excavator - ECU P/N: 728/35700"
"Geo Prism 1995 - ABS #16213899"
"Geo Prism 1995 - ABS #16213899"
"Geo Prism 1995 - ABS #16213899"
"Wie man BBA reman erreicht"
"this test JCB"
"Ersatz Airbags, Gurtstrammer und Auto Körper Teile"

现在我想用我的搜索词查询列表,比如geo jcb

如果你看,那么geo这个词已经在描述字段中存储了很多次。所以我想以这样的方式对我的列表进行排序,以使搜索词中的单词找到最大的数据将首先出现。请帮我这样做。谢谢

4

4 回答 4

2

您可以将string.SplitandEnumerable.OrderByDescending与匿名类型一起使用:

List<SearchResult> list = new List<SearchResult>() { 
    new SearchResult(){Description="JCB Excavator - ECU P/N: 728/35700"},
    new SearchResult(){Description="Geo Prism 1995 - ABS #16213899"},
    new SearchResult(){Description="Geo Prism 1995 - ABS #16213899"},
    new SearchResult(){Description="Geo Prism 1995 - ABS #16213899"},
    new SearchResult(){Description="Wie man BBA reman erreicht"},
    new SearchResult(){Description="this test JCB"},
    new SearchResult(){Description="Ersatz Airbags, Gurtstrammer und Auto Körper Teile"},
};

string[] searchTerms = new[]{"geo", "jcb"};
var results = 
    list.Select(sr => new { Searchresult = sr, Words = sr.Description.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) })
        .OrderByDescending(x => x.Words.Count(w => searchTerms.Contains(w.ToLower())))
        .Select(x => x.Searchresult);
于 2012-07-09T09:19:30.980 回答
1

您可以使用简单的正则表达式,只需将模式中的搜索词与|

var re = new Regex("geo|JCB",RegexOptions.IgnoreCase);

然后计算描述中的匹配数:

Console.WriteLine(re.Matches(description).Count); // Outputs '5' in your example

您可以通过以下方式订购您的清单:

searchResults.OrderByDescending(r => re.Matches(r).Count);

现场示例:http ://rextester.com/MMAT58077


编辑:根据您在评论中链接的新问题(希望您更新此问题的详细信息并让重复项消失)您希望对结果进行排序,以便最常见的结果显示在结果列表的前面.

为此,您可以首先计算每个搜索短语的相关权重,并使用它对结果进行排序。

Step1:通过统计每个搜索词在整个数据集中出现的总次数来计算权重:

var wordsToFind = "Geo JCB".Split();
// find number of times each search phrase is found
var weights = wordsToFind.Select( w => new { 
         Word = w, 
         Weight = list.Where(x => x.Description.Contains(w)).Count() 
    } );

对于目前这个问题中的数据,这给出了结果:

GEO: 3
JCB: 2

因此,您首先想要所有GEO结果,然后是JCB. 我想最好的办法是让第一个结果成为GEO最常被提及的结果。

步骤 2:使用在步骤 1 中计算的权重对搜索结果进行排序。

var values = list.Select(x => new { 
      SearchResult = x, 
      Words = x.Description.Split(' ')
   })
   .Select(x => new { 
       SearchResult = x.SearchResult, 
       Weight = weights.Sum(w => x.Words.Contains(w.Word) ? w.Weight : 0)
   })
   .OrderByDescending(x => x.Weight)
   .Select(x => x.SearchResult);

现场示例:http ://rextester.com/SLH38676

于 2012-07-09T09:16:53.767 回答
1
List<SearchResult> list = new List<SearchResult>() 
{ 
   new SearchResult { Description = "JCB Excavator - ECU P/N: 728/35700" },
   new SearchResult { Description = "Geo Prism 1995 - ABS #16213899" },
   new SearchResult { Description = "Geo Prism 1995 - ABS #16213899" },
   new SearchResult { Description = "Geo Prism 1995 - ABS #16213899" },
   new SearchResult { Description = "Wie man BBA reman erreicht" },
   new SearchResult { Description = "this test JCB" },
   new SearchResult { Description = "Ersatz Airbags, Gurtstrammer und Auto Körper Teile" }            
   };

   var wordsToFind = "Geo JCB".Split();
   var values = list.Select(x => new { SearchResult = x, Count = x.Description.Split(' ')
                                             .Where(c => wordsToFind .Contains(c)).Count() })
                    .OrderByDescending(x => x.Count)
                    .Select(x => x.SearchResult);
于 2012-07-09T09:17:33.807 回答
0
var results = db.Blogs.AsEnumerable()
    .Select(sr => new
    {
        Searchresult = sr,
        Words = Regex.Split(sr.Name, @"[^\S\r\n {1,}").Union(Regex.Split(sr.Name2, @"[^\S\r\n]{1,}"))
    })
    .OrderByDescending(x => x.Words.Count(w => {
        foreach (var item in searchTerms)
        {
            if(w.ToLower().Contains(item))
            {
                return true;
            }
        }
        return false;
    }))
    .Select(x => x.Searchresult);
于 2012-09-20T22:40:10.930 回答