LINQ 计算出现次数
我有以下效果很好的查询:
string[] Words = {"search","query","example"};
... Snip ...
var Results = (
from a in q
from w in Words
where
(
a.Title.ToLower().Contains(w)
|| a.Body.ToLower().Contains(w)
)
select new
{
a,
Count = 0
}).OrderByDescending(x=> x.Count)
.Distinct()
.Take(Settings.ArticlesPerPage);
我需要它做的是返回 Count
这是单词出现的总数。我也会根据标题来衡量它,例如:
Count = (OccuranceInTitle * 5) + (OccurancesInBody)
我假设我需要使用 Linq.Count 但我不确定如何在这种情况下应用它。
I have the following query which works great:
string[] Words = {"search","query","example"};
... Snip ...
var Results = (
from a in q
from w in Words
where
(
a.Title.ToLower().Contains(w)
|| a.Body.ToLower().Contains(w)
)
select new
{
a,
Count = 0
}).OrderByDescending(x=> x.Count)
.Distinct()
.Take(Settings.ArticlesPerPage);
What I need it to do, is return Count
which is the total occurrences of the words. I'm going to weight it in favour of the title as well, example:
Count = (OccuranceInTitle * 5) + (OccurancesInBody)
I'm assuming I need to use the Linq.Count but I'm not sure how to apply it in this instance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是我想到的:
对出现次数进行计数是通过(令人惊讶的)快速而肮脏的方法完成的,即用
string.Empty
替换出现次数并根据结果字符串长度进行计算。计算出每篇文章和每个单词的分数后,我对每篇文章进行分组,按所有单词的分数总和排序,并从结果中取出一部分。我没有启动编译器,所以请原谅任何明显的错误。
更新:此版本使用正则表达式,而不是
原始版本的
正则表达式,因此它现在仅匹配整个单词(
string.Replace
版本也将匹配单词片段)。This is what I came up with:
Counting occurrences is done with the (surprisingly) quick and dirty method of replacing occurrences with
string.Empty
and calculating based on the resulting string length. After the scores for each article and each word are calculated, I 'm grouping for each article, ordering by the sum of scores for all the words and taking a chunk out of the results.I didn't fire up the compiler, so please excuse any obvious mistakes.
Update: This version uses regexes as in
instead of the original version's
so that it now matches only whole words (the
string.Replace
version would also match word fragments).