2017-03-02 101 views
0

我有兩個對象列表,我需要加入Sku然後再計算SumQty * Price)。我不確定如何做到這一點?以下是我迄今爲止:Linq查詢加入兩個對象並計算總和

var profit = (from order in orders 
      join cost in costs 
      on order.Sku equals cost.Sku 
      select (order.Qty * cost.Price)).Sum(); 

從這裏摘自:

List<Order> orders = new List <Order> 
{ 
    new Order() { Sku = "ABC", Qty = 1 }, 
    new Order() { Sku = "XYZ", Qty = 2} 
}; 

List<Cost> costs = new List <Cost> 
{ 
    new Cost() { Sku = "ABC", Price = 4.50m }, 
    new Cost() { Sku = "XYZ", Price = 2.25m } 
}; 


var profit = from order in orders 
      join cost in costs 
      on order.Sku equals cost.Sku 
      select Sum(order.Qty * cost.Price) 
+0

到底哪裏出問題了? – Chris

+0

它不喜歡最後一行'選擇總和(order.Qty * cost.Price)' – PixelPaul

+0

如果沒有完全1:1的'Order'和'Cost'對象會怎麼樣?我假設你將需要一些分組。 –

回答