2017-05-11 93 views
2

是否有可能在linq中執行以下操作,在我的select new中使用TotalOrderItems的值作爲TotalInStockItems的計算的一部分?Linq使用選擇新值來計算另一個字段

var Order = (from o in orderItems 
      select new ShippingOrder { OrderId = orderItems.OrderId 
             TotalOrderItems = orderItems.GroupBy(x => x.Sku).Count(), 
             TotalInStockItems = TotalOrderItems - orderItems.Where(x => x.InStock =='F').GroupBy(x => x.Sku).Count(), 
             }).ToList(); 
+0

您可能需要查看你的問題的例子。它看起來不完整 – Nkosi

回答

2

嘗試使用let clause (C# Reference)來使用的值存儲在後面的查詢

(from o in orderItems 
let totalOrderItems = orderItems.GroupBy(x => x.Sku).Count() 
select new ShippingOrder { 
    OrderId = o.OrderId 
    TotalOrderItems = totalOrderItems, 
    TotalInStockItems = totalOrderItems - orderItems.Where(x => x.InStock =='F').GroupBy(x => x.Sku).Count(), 
}).ToList(); 
+0

完美,正是我所需要的。不知道「讓」。謝謝! – PixelPaul

相關問題