我使用物體框架在資料庫中創建了三個表
class User{
public string Name {get;set;}
public IEnumerable<Pet> Pets {get;set;}
}
class Pet{
public string Name {get;set;}
public IEnumerable<Toy> Toys {get;set;}
}
class Toy{
public string Name {get;set;}
}
我想選擇玩具數量最多的前 10 個用戶(使用 linq)。
試過這個,但沒有奏效。物體框架無法將其轉換為 SQL 查詢
Entities
.OrderBy(u => u.Pets.Select(n => n.Toys.Count()))
.ToListAsync();
我應該使用哪種 linq 查詢來執行此操作?
uj5u.com熱心網友回復:
我建議用總計準備分組查詢:
var totals =
from e in Entities
from p in e.Pets
from t in p.Toys
group e by e.Id into g
select new
{
Id = g.Key,
Count = g.Count()
};
var query =
from e in Entities
join t in totals on e.Id equals t.Id
orderby t.Count descending
select e;
var result = await query
.Take(10)
.ToListAsync();
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/494222.html
標籤:C# 实体框架 林克 sql-order-by
上一篇:C#LINQ如何獲取最大值
