假设我们有这样的数据库设计。
Customer
Id Name
1 John
2 Jack
Order
Id CustomerId
1 1
2 1
3 2
OrderLine
Id OrderId ProductId Quantity
1 1 1 10
2 1 2 20
3 2 1 30
4 3 1 10
如何创建实体框架查询来计算给定客户订购给定产品的总数量?
Input => CustomerId = 1 & ProductId = 1
Output => 40
这就是我到目前为止所得到的,因为它不完整并且仍然缺少总和。
var db = new ShopTestEntities();
var orders = db.Orders;
var details = db.OrderDetails;
var query = orders.GroupJoin(details,
order => order.CustomerId,
detail => detail.ProductId,
(order, orderGroup) => new
{
CustomerID = order.CustomerId,
OrderCount = orderGroup.Count()
});