Sort List by occurrence of a word by LINQ C#

后端 未结 4 781
我寻月下人不归
我寻月下人不归 2020-12-18 16:25

i have stored data in list like

 List list = new List();
 SearchResult sr = new SearchResult();
 sr.Description = \"s         


        
4条回答
  •  时光取名叫无心
    2020-12-18 16:42

    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

提交回复
热议问题