【发布时间】:2021-12-14 06:50:02
【问题描述】:
我想获得他们列表中最多的项目 我要运行的 sql 查询
select
i.Id,
count(*) as count
from Items
inner join ItemItemList il on i.Id = il.ItemsId
group by i.Id
order by count desc
物品实体
public class Item:BaseEntity
{
public string Name { get; set; }
public decimal Price { get; set; }
public decimal DiscountedPrice{ get; set; }
public virtual ICollection<ItemList> ItemLists { get; set; }
}
项目列表实体
public class ItemList:BaseEntity
{
public string Name { get; set; }
public string Description { get; set; }
public int UserId { get; set; }
public ICollection<Item> Items { get; set; }
[ForeignKey("UserId")]
public virtual User User { get; set; }
}
我的 DTO
public class TopItemsInLists
{
[BsonRepresentation(BsonType.ObjectId)]
[BsonId]
public string ItemId { get; set; }
public int Quantity { get; set; }
}
我的项目仓库
var query = _context.Items.Include(l => l.ItemLists)
.GroupBy(g => g.ItemLists)
.Select(z => new TopItemsInLists { ItemId = z.Key.ToString(), Quantity = z.Count() })
.OrderByDescending(z => z.Quantity)
.Take(10);
我想在 ItemLists 中找到最多的项目。 我在哪里做错了?如果有人有其他建议
【问题讨论】:
标签: asp.net-mvc linq .net-core