1 回答
TA贡献2036条经验 获得超8个赞
如果需要获取所有产品,则应查询产品表:
public IActionResult LSProducts()
{
List<ProductSoldViewModel> model = new List<ProductSoldViewModel>();
model = _context.Product
.Include(a => a.OrderDetails)
.Select(o => new ProductSoldViewModel
{
ProductCode = o.CodProduct,
ProductName = o.Nome,
Qty = o.OrderDetails.Sum(s => s.Qty)
})
.OrderBy(od => od.Qty)
.ToList();
return View(model);
}
为了避免空异常,您可能需要将以下构造函数添加到您的模型中:
public class Product
{
public Product()
{
OrderDetails = new HashSet<OrderDetail>();
}
(...)
public ICollection<OrderDetail> OrderDetails { get; set; }
}
- 1 回答
- 0 关注
- 154 浏览
添加回答
举报