【发布时间】:2021-12-14 09:30:57
【问题描述】:
我的场景:用户将能够创建列表并将项目添加到这些列表中。我要做的是最多在用户创建的列表中找到项目。
物品实体
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; }
}
用户实体
public class User:BaseEntity
{
public string Name { get; set; }
public string Surname { get; set; }
public string Gsm { get; set; }
public string Email { get; set; }
public virtual ICollection<ItemList> ItemLists{ get; set; }
}
我的 DTO
public class TopItemsForUsers
{
[BsonRepresentation(BsonType.ObjectId)]
[BsonId]
public string ItemId { get; set; }
public string UserId { 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);
我想获得在用户列表中非常常见的产品 我在哪里做错了?如果有人有其他建议
【问题讨论】:
-
那么,查询的根应该是用户?
-
我的场景:用户将能够创建列表并将项目添加到这些列表中。我要做的是最多在用户创建的列表中找到项目。
-
我想在用户创建的列表中找到最常见的项目。我想我的根应该是用户,但我不确定
-
“最多”是什么意思? “非常在场”是什么意思?
标签: c# entity-framework linq asp.net-core