如何根据每行 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();
我从 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
最佳答案
在这一行之后:
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();
}
关于c# - 按列表行中单词的出现次数排序 linq,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11401622/