[英]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();
这将按 CategoryID 对您的博客进行分组,按每个分组的计数排序,取前 3 个,然后 select 将每个组的类别名称和计数作为一个列表。
list.GroupBy(x => x.CategoryName).Select(x => new { x.Key, count = x.Count() }).OrderBy(x => x.count).Take(3);
这将首先按名称对项目进行分组。 然后使用组键(名称)和每个组中所有项目的计数创建一个匿名 object。 然后你按数量排序并取前 3 个。
您可以按类别名称分组,首先按最高计数排序,然后选择前三个结果。 例子:
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.