所以我有一个 IEnumerable CallsForProperty 我需要找到特定时间段的前 3 个问题,所以我创建了 3 个字典 Dictionary<int, string> FAQfor3Months = null; Dictionary<int, string> FAQfor4to12Months = null; Dictionary<int, string> FAQfor12plusMonths = null; foreach(Activity call in CallsForProperty){ if( /*call is in 3 months*/ ){ //do some unrelated stuff FAQfor3Months.Add(counter, call.callQuestion) counter++; } }我有 2 个问题,我试图找出最常见的 3 个问题,这个查询正确吗?var threeMonthFaqs = FAQfor3Months.GroupBy(x => x.Value).OrderByDescending(x => x.Count()).Take(3); 这也太低效了吧?有没有更好的方法来查询集合?
2 回答
慕的地6264312
TA贡献1817条经验 获得超6个赞
反转Dictionarys 并计算遇到的每个问题,然后可以使用 LINQ 找出最重要的 3 个问题:
var FAQfor3Months = new Dictionary<string, int>();
var FAQfor4to12Months = new Dictionary<string, int>();
var FAQfor12plusMonths = new Dictionary<string, int>();
foreach (Activity call in CallsForProperty) {
if ( /*call is in 3 months*/ ) {
//do some unrelated stuff
FAQfor3Months.TryGetValue(call.callQuestion, out int count);
FAQfor3Months[call.callQuestion] = count+1;
}
}
var top3ThreeMonthFaqs = FAQfor3Months.OrderByDescending(kvp => kvp.Value).Take(3).Select(kvp => kvp.Key).ToList();
繁星点点滴滴
TA贡献1803条经验 获得超3个赞
您的代码将返回组,而不是实际的问题。尝试这个:
var threeMonthFaqs = FAQfor3Months.Values .GroupBy(q => q) .OrderByDescending(g => g.Count()) .SelectMany(g => g) .Distinct() .Take(3);
- 2 回答
- 0 关注
- 93 浏览
添加回答
举报
0/150
提交
取消