15

我想为每个客户选择一个订单项目的所有(付费)价格的总和。这是SQL命令:

SELECT c.name,SUM(oi.price * oi.count) from customer c
JOIN order o ON c.id=o.customer_id
JOIN order_item oi ON o.id=oi.order_id
JOIN bill b ON b.id=oi.bill_id
WHERE b.payment_id is NOT null
GROUP by c.name;

我不知道如何在 EF 中执行此操作。示例结果:

John Smith  1500,2  
Allan Babel 202,0  
Tina Crown  3500,78

(逗号用作小数点..因为价格是十进制值)

4

1 回答 1

29

您的示例结果似乎与您的 SQL 命令不匹配,但我认为您正在寻找这样的东西:

var query = from c in context.Customers
            join o in context.Orders on c.id equals o.customer_id
            join oi in context.OrderItems on o.id equals oi.order_id
            join b in context.bill on oi.bill_id equals b.id
            where b.payment_id != null
            group oi by c.name into g
            select new
            {
              Name = g.Key,
              Sum = g.Sum(oi => oi.price * oi.count),
            }
于 2012-04-25T13:40:46.110 回答