使用LINQ选择最常用的值

war*_*rdh 19 c# sql linq entity-framework

我正在尝试在表中选择前五个最常用的值并将它们返回到List中.

    var mostFollowedQuestions = (from q in context.UserIsFollowingQuestion
                                 select *top five occuring values from q.QuestionId*).toList();
Run Code Online (Sandbox Code Playgroud)

任何的想法?

谢谢

saj*_*saj 40

        var mostFollowedQuestions = context.UserIsFollowingQuestion
                                    .GroupBy(q => q.QuestionId)
                                    .OrderByDescending(gp => gp.Count())
                                    .Take(5)
                                    .Select(g => g.Key).ToList();
Run Code Online (Sandbox Code Playgroud)


Tim*_*oyd 30

 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);
Run Code Online (Sandbox Code Playgroud)