我想为每个客户选择一个订单项的所有(支付)价格的总和。
这是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


(逗号用作小数点。由于价格为小数)

最佳答案

您的示例结果似乎与您的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),
            }

关于entity-framework - Entity Framework 中的GROUP和SUM,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10316648/

10-10 07:15