3

如何按每行 linq 数据中出现的单词对列表进行排序?我从这里得到了一个给出正确输出的人的答案。这是代码:

void Main()
{
    List<SearchResult> list = new List<SearchResult>() { 
        new SearchResult(){ID=1,Title="Geo Prism GEO 1995 GEO* - ABS #16213899"},
        new SearchResult(){ID=2,Title="Excavator JCB - ECU P/N: 728/35700"},
        new SearchResult(){ID=3,Title="Geo Prism GEO 1995 - ABS #16213899"},
        new SearchResult(){ID=4,Title="JCB Excavator JCB- ECU P/N: 728/35700"},
        new SearchResult(){ID=5,Title="Geo Prism GEO,GEO 1995 - ABS #16213899 GEO"},
        new SearchResult(){ID=6,Title="dog"},
    };

    var to_search = new[] { "Geo", "JCB" };

    var result = from searchResult in list
         let key_string = to_search.FirstOrDefault(ts =>  searchResult.Title.ToLower().Contains(ts.ToLower()))
         group searchResult by key_string into Group
         orderby Group.Count() descending
         select Group;
         result.ToList().Dump();



 }
// Define other methods and classes here
public class SearchResult
{
    public int ID { get; set; }
    public string Title { get; set; }
}

我得到的输出像

ID Title 
-- ------
1  Geo Prism GEO 1995 GEO* - ABS #16213899 
3  Geo Prism GEO 1995 - ABS #16213899 
5  Geo Prism GEO,GEO 1995 - ABS #16213899 GEO 
2  Excavator JCB - ECU P/N: 728/35700 
4  JCB Excavator JCB- ECU P/N: 728/35700 
6  dog 

上面的输出没问题。所有具有 ORD GEO 的行都排在第一位,因为它在大多数行中找到了最大时间,这意味着 GEO 是在 3 行中找到的单词,而 JCB 在两行中找到,因此接下来是与 JCB 相关的行。

在获得整个数据的上述输出后,我需要另一种排序。即 GEO 行排在第一位,哪一行具有 GEO 字的最大时间。所以我的输出如下所示:

ID Title 
-- ------
5  Geo Prism GEO,GEO 1995 - ABS #16213899 GEO 
1  Geo Prism GEO 1995 GEO* - ABS #16213899 
3  Geo Prism GEO 1995 - ABS #16213899 
4  JCB Excavator JCB- ECU P/N: 728/35700 
2  Excavator JCB - ECU P/N: 728/35700 
6  dog 

我发现了一个 linq 查询,它计算字符串中某个单词的出现次数:

string text = @"Historically, the world of data and data the world of objects data" ;
string searchTerm = "data";
//Convert the string into an array of words
string[] source = text.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' },   StringSplitOptions.RemoveEmptyEntries);
var matchQuery = from word in source
             where word.ToLowerInvariant() == searchTerm.ToLowerInvariant()
             select word;
int wordCount = matchQuery.Count();

我从这个网址得到它

如何使用上面的代码对我的标题进行排序?如何使用第二种排序来计算标题字段中单词的出现次数,结果我的输出如下所示:

ID Title 
-- ------
5  Geo Prism GEO,GEO 1995 - ABS #16213899 GEO 
1  Geo Prism GEO 1995 GEO* - ABS #16213899 
3  Geo Prism GEO 1995 - ABS #16213899 
4  JCB Excavator JCB- ECU P/N: 728/35700 
2  Excavator JCB - ECU P/N: 728/35700 
6  dog 
4

3 回答 3

1

使用 WordCount 作为字符串的扩展方法,然后可以使用简单的 Lambda 表达式:

list.OrderByDescending(sR => sR.Title.WordCount( to_search ))

如果您想省略所有没有任何搜索词的结果,您可以使用该Where子句。IE

IEnumerable<SearchResult> results = list
                .Where( sR => sR.Title.WordCount( searchTerms ) > 0 )
                .OrderByDescending( sR => sR.Title.WordCount( searchTerms ) );

编辑 如果搜索词具有优先级,您可以像这样对每个项目进行多次排序(首先按优先级最低的元素排序,然后是下一个,直到最终排序在具有最高优先级的项目上):

string[] searchTerms = new string[]{ "GEO","JCB" };
IEnumerable<SearchResult> results = list;
foreach( string s in searchTerms.Reverse() ) {
    results = results
        .OrderByDescending( sR => sR.Title.WordCount( s ) );
}

扩展方法:

static class StringExtension{
        public static int WordCount( this String text, string searchTerm )
        {
            string[] source = text.Split( new char[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries );
            var matchQuery = from word in source
                             where word.ToLowerInvariant() == searchTerm.ToLowerInvariant()
                             select word;
            int wordCount = matchQuery.Count();
            return wordCount;
        }
        public static int WordCount( this String text, IEnumerable<string> searchTerms ) {
            int wordCount = 0;
            foreach( string searchTerm in searchTerms ) {
                wordCount += text.WordCount( searchTerm );
            }
            return wordCount;
        }
    }
于 2012-07-09T19:57:04.263 回答
1

在这一行之后:

var result = from searchResult in list
         let key_string = to_search.FirstOrDefault(ts =>  searchResult.Title.ToLower().Contains(ts.ToLower()))
         group searchResult by key_string into Group
         orderby Group.Count() descending
         select Group;

你想要这样的东西:

foreach (var group in result) {
      foreach (var item in group.OrderByDescending(theItem => WordCount(theItem.Title, group.Key))) {
          Console.WriteLine(item.Title);
      }
}

使用如下所示的添加方法:

public static int WordCount(string haystack, string needle) {
    if (needle == null) {
        return 0;
    }
    string[] source = haystack.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries);
    var matchQuery = from word in source
                        where word.ToLowerInvariant() == needle.ToLowerInvariant()
                        select word;
    return matchQuery.Count();
}
于 2012-07-09T19:58:47.483 回答
1

这个怎么样:

IEnumerable<SearchResult> result =
    from searchResult in list
    let key_string = to_search.FirstOrDefault(ts => searchResult.Title.ToLower().Contains(ts.ToLower()))
    group searchResult by key_string into Group
    orderby Group.Count() descending
    from item in Group.OrderByDescending(theItem => WordCount(theItem.Title, Group.Key))
    select item;

使用以下WordCount方法:

public static int WordCount( String text, string searchTerm )
{
    string[] source = text.Split( new char[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries );
    var matchQuery = from word in source
                     where word.ToLowerInvariant() == searchTerm.ToLowerInvariant()
                     select word;
    int wordCount = matchQuery.Count();
    return wordCount;
}

我注意到的一个小问题是不包含匹配单词的标题将被组合在一起,因此可以将它们放在匹配单词的标题前面。

于 2012-07-10T13:58:31.137 回答