我在文本分析方面遇到了麻烦。 需要计算 .txt 文件中的单词迭代次数 我做到了,但我需要将所有计数放在一起而不分开文件。
如何正确分组重写?
var queryMatchingFiles =
from file in files
where file.Extension == ".txt"
let fileText = File.ReadAllText(file.FullName)
let matches = Regex.Matches(fileText, searchTerm)
select new
{
matchedValues = from Match match in matches
group match by match.Value into grp
select new {
key = grp.Key, value = grp.Count()
}
};
最佳答案
您的查询应如下所示:
var queryMatchingFiles =
from file in files
where file.Extension == ".txt"
let fileText = File.ReadAllText(file.FullName)
from match in Regex.Matches(fileText, searchTerm).Cast<Match>()
group match by match.Value into grp
select new
{
key = grp.Key,
value = grp.Count()
};
创建IEnumerable<Match>
来自您可以使用的比赛:
Regex.Matches(fileText, searchTerm).Cast<Match>()
所以你可以写类似 from match in ...
的查询
另一个转换选项是在表达式中显式指定类型:
from Match match in Regex.Matches(fileText, searchTerm)
关于c# - 如何在 C# (linq) 上创建困难组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49519373/