2017-07-15 73 views
1

Relationship Diagram总和列使用LINQ to SQL的

在为每一个客户我要选择所有订单上面的图来获得计算的值,然后为每个订单我计算所有的TotalPrice =(总和食品包括在订单*数量)+ ExtraPrice。我正在努力使用linq to sql为它创建一个查询。

回答

0
var res = (from a in dc.orders 
        join b in dc.orderLines on a.orderId equals b.fk_orderId 
        join c in dc.foodItems on b.fk_foodId equals c.foodId 
        where a.fk_custId == cID 
        group new { a,c,b } by a into g 
        select new 
        { 
         OID1 = g.Key.orderId, 
         date1 = g.Key.date.Value.Date, 
         price1 = g.Sum(x => x.c.price * x.b.quantity) + g.Key.orderLines.Select(o => o.extraPrice).Sum() 
        }); 

上面给出的LINQ查询我一直在寻找。

0

应该接近这个。我不在计算机旁测试,所以让我知道你是否有错误。

db.orders.Select(o => new { o.orderid, o.date, TotalPrice = ((o.orderLines.Select(ol => ol.food items.Count()).Sum() * o.Quantity) + o.extraPrice) }) 
+0

它在ol.foodItems.Count()上显示错误。不包含count的定义。 – Faisal

+0

,我还必须为客户ID指定where子句。 – Faisal

+0

dc.orders.Select(o => new {o.orderId,o.date,TotalPrice =((o.orderLines.Select(ol => ol.foodItem.price).Sum()* decimal.Parse(o。 orderLines.Select(ox => ox.quantity).ToString()))+ decimal.Parse(o.orderLines.Select(x => x.extraPrice).ToString()))}); 试过这个,它会抛出异常。无法翻译。 – Faisal