2017-07-15 59 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