排序列出了一个词的出现通过LINQ C# [英] Sort List by occurrence of a word by LINQ C#

查看:80
本文介绍了排序列出了一个词的出现通过LINQ C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经存储在列表数据,如

 列表<&SearchResult中GT;名单=新名单,LT;信息搜索结果>(); 
SearchResult中SR =新的信息搜索结果();
sr.Description =样本描述;
list.Add(SR);



想我的数据存储在描述字段如

 JCB挖掘机 -  ECU P / N:35700分之728
地理棱镜1995 - ABS#16213899
地理棱镜1995 - ABS#16213899
地理棱镜1995 - ABS#16213899
小魏人BBA再制造erreicht
这个测试JCB
仿照安全气囊,Gurtstrammer UND自动KörperTEILE

现在我想和喜欢的地理JCB

如果你看看那么这个词的地理位置已经存储了多次在描述字段。所以我想我的排序,以这样一种方式列表,在搜索词词发现最大数据将是第一位的。请帮我这样做。谢谢


解决方案

您可以使用一个简单的正则表达式,只需用结合您的搜索字词的格局|

  VAR重=新的正则表达式(地理| JCB,RegexOptions.IgnoreCase); 



再算上在你的​​描述匹配的数量:

  Console.WriteLine(re.Matches(介绍).Count之间); //输出'5'在你的榜样

您可以通过这个命令列表:



  searchResults.OrderByDescending(R = GT; re.Matches(R).Count之间); 



活生生的例子:的 http://rextester.com/MMAT58077






修改 :根据你在评论中联新问题(希望你会更新这个问题的细节,让重复的模具)要订购的结果,以便在最常见的结果在结果列表中较早的显示出来。



要做到这一点,你可以先计算每个搜索短语相关的权重,并使用此命令的结果。



第一步:通过计算总次数每个搜索词出现在整个数据集的计算权重:

  VAR wordsToFind =地理JCB.Split(); 
//找到的次数每个搜索短语被发现
无功权重= wordsToFind.Select(W =>新建{
字= W,
重量= list.Where( X => x.Description.Contains(W))计数()
})。

有关在当下这个这个问题上的数据givves结果:

  GEO:3 
JCB:2

所以,你希望所有的 GEO 结果第一,其次是 JCB 。我想一个不错的到了将有第一个结果是其中提到的 GEO 频率最高的之一。



第二步:使用在步骤1中计算出的加权下令搜索结果

  VAR值= list.Select (X =>新建{
SearchResult中= X,
字= x.Description.Split('')
})
。选择(X =>新建{
SearchResult中= x.SearchResult,
重量= weights.Sum(W => x.Words.Contains(w.Word)w.Weight:0)
})
.OrderByDescending(X => x.Weight)
。选择(X => x.SearchResult);



活生生的例子:的 http://rextester.com/SLH38676


i have stored data in list like

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

suppose my data is stored in description field like

"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"

now i want to query the list with my search term like geo jcb

if you look then the word geo has stored many times in the description field. so i want to sort my list in such way that the word in search term found maximum that data will come first. please help me to do so. thanks

解决方案

You could use a simple regular expression, just combine your search terms in the pattern with |:

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

Then count the number of matches in your description:

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

You could order your list by this:

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

Live example: http://rextester.com/MMAT58077


Edit: According to your new question linked in the comments (and hopefully you'll update the details of this question and let the duplicate die) you wish to order the results so that the most common result shows up earlier on in the list of results.

To do this, you could first calculate the relevant weighting of each search phrase, and use this to order the results.

Step1: Calculate the weighting by counting the total number of times each search word appears in the entire set of data:

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

For the data in this question at the moment this givves the result:

GEO: 3
JCB: 2

So you want all the GEO results first, followed by JCB. I guess a nice-to-have would be to have the first result be the one where GEO is mentioned most often.

Step2: Use the weightings calculated in step 1 to order the results of a search.

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

Live example: http://rextester.com/SLH38676

这篇关于排序列出了一个词的出现通过LINQ C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆