如何根据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 上面的输出是ok的。具有顺序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();我是从this url买的
如何使用上面的代码对标题进行排序?如何使用第二个排序来计算一个单词在标题字段中的出现次数,结果我的输出将如下所示:
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 发布于 2012-07-10 03:58:47
在此行之后:
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-10 03:57:04
使用WordCount作为字符串的扩展方法,然后可以使用简单的Lambda表达式:
list.OrderByDescending(sR => sR.Title.WordCount( to_search ))如果您想省略所有没有任何搜索词的结果,可以使用Where子句。也就是说。
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-10 21:58:31
这样如何:
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;
}我注意到的一个小问题是,不包含匹配词的标题将被组合在一起,因此可以将它们放在具有匹配词的标题之前。
https://stackoverflow.com/questions/11401622
复制相似问题