I'm trying to select the top five most frequent values in my table and return them in a List.
我正在尝试选择表中最常见的5个值,并在列表中返回它们。
var mostFollowedQuestions = (from q in context.UserIsFollowingQuestion
select *top five occuring values from q.QuestionId*).toList();
Any idea?
任何想法?
Thanks
谢谢
2 个解决方案
#1
36
var mostFollowedQuestions = context.UserIsFollowingQuestion
.GroupBy(q => q.QuestionId)
.OrderByDescending(gp => gp.Count())
.Take(5)
.Select(g => g.Key).ToList();
#2
26
int[] nums = new[] { 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7 };
IEnumerable<int> top5 = nums
.GroupBy(i => i)
.OrderByDescending(g => g.Count())
.Take(5)
.Select(g => g.Key);
#1
36
var mostFollowedQuestions = context.UserIsFollowingQuestion
.GroupBy(q => q.QuestionId)
.OrderByDescending(gp => gp.Count())
.Take(5)
.Select(g => g.Key).ToList();
#2
26
int[] nums = new[] { 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7 };
IEnumerable<int> top5 = nums
.GroupBy(i => i)
.OrderByDescending(g => g.Count())
.Take(5)
.Select(g => g.Key);