我在订单和产品之间使用m-m关系。
Product_Order:
ProductId
OrderId
我很难纠正以下这一行:
ProductAmount = db.Orders.Where(o=> o.Id == id).Sum(o => o.Products.Count(p=> p.Id == x.Id))
我正在尝试阅读当前Order
并在该订单中获得相同Product
的金额。
public ActionResult GetOrder(Guid id) {
var currentOrder = db.Orders.SingleOrDefault(x => x.Id == id);
var returnOrder = new {
FirstName = currentOrder.User.FirstName,
LastName = currentOrder.User.LastName,
Address = currentOrder.User.Address,
TotalPrice = currentOrder.TotalPrice,
OrderDate = currentOrder.OrderDate,
Products = currentOrder.Products.Select(x => new {
ProductName = x.ProductName,
ProductAmount = db.Orders.Where(o => o.Id == id).Sum(o => o.Products.Count(p => p.Id == x.Id))
})
};
return Json(returnOrder);
}
如果您能纠正ProductAmount
行,我将不胜感激。
---附加代码:
$.post("/Home/GetOrder", { "id": orderId },
function (data) {
if (data) {
$.each(data.Products, function(index, item) {
Alert(item.ProductAmount);
});