我在源表中有資料。

我想使用 lambda 運算式和物體框架在串列中獲得這種結果。我不知道該怎么做。我需要為每個 CategoryId 獲取前 3 行。

可能使用這樣的東西:
context.GroupBy(x => x.CategoryId)
.Select(group => new {
CategoryId = group.Key,
NameId = group.Select(x => x.NameId),
group.OrderByDescending(e => e.Count).Take(3)
})
.ToListAsync()
uj5u.com熱心網友回復:
var list = context
.GroupBy(x => x.CategoryId)
.SelectMany(group => group.OrderByDescending(e => e.Count).Take(3))
.OrderByDescending(e => e.Count)
.ToListAsync();
如果你想要一個匿名型別:
var list = context
.GroupBy(x => x.CategoryId)
.SelectMany(group => group.OrderByDescending(e => e.Count).Take(3))
.Select(x => new
{
x.CategoryId,
x.NameId,
x.Count
})
.OrderByDescending(x => x.Count)
.ToListAsync();
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/426636.html
下一篇:檢測到可能的物件回圈
