LINQ is used to find the most common value.

c# entity-framework linq sql

Question

I'm trying to select the top five most frequent values in my table and return them in a List.

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

Any idea?

Thanks

1
20
7/18/2011 9:33:05 AM

Accepted Answer

var mostFollowedQuestions = context.UserIsFollowingQuestion
                                    .GroupBy(q => q.QuestionId)
                                    .OrderByDescending(gp => gp.Count())
                                    .Take(5)
                                    .Select(g => g.Key).ToList();
41
1/30/2020 9:42:40 PM

Popular Answer

 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);


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow