[英]How to select the most repeating records in related tables with Entity Framework
var result = blogs.GroupBy(b => b.CategoryID)
.OrderByDescending(g => g.Count())
.Take(3)
.Select(x => new {CategoryName = x.First().Category.CategoryName, Count = x.Count()})
.ToList();
This will group your blogs by CategoryID, order by count of each grouping, take the top 3 and then select the category name and count of each group as a list.这将按 CategoryID 对您的博客进行分组,按每个分组的计数排序,取前 3 个,然后 select 将每个组的类别名称和计数作为一个列表。
list.GroupBy(x => x.CategoryName).Select(x => new { x.Key, count = x.Count() }).OrderBy(x => x.count).Take(3);
This will first Group the items by the name.这将首先按名称对项目进行分组。 Then create an anonymous object with the group key (the name) and the Count of all items in every group.然后使用组键(名称)和每个组中所有项目的计数创建一个匿名 object。 Then you order by count and take the first 3.然后你按数量排序并取前 3 个。
You could group by category name, order by highest count first and pick the first three results.您可以按类别名称分组,首先按最高计数排序,然后选择前三个结果。 Example:例子:
Blogs
.GroupBy(b => b.Category.CategoryName)
.OrderByDescending(g => g.Count())
.Take(3)
.Select(x => new { CategoryName = x.Key, Count = x.Count() });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.