[英]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.