2016-12-31 183 views
0

我正在計算基於股票交易的庫存,但我想排除那些發票未標記爲的股票交易IsStocked這是Nullable布爾值,我試圖通過使用SkipWhile這是行不通的,它簡單地計算所有交易的無論是發票IsStocked屬性爲true或false:C#Linq To Entities(SkipWhile not working)

這裏是我的嘗試:

public double GetInStockQuantity(Warehouse warehouse, Entities db) 
{ 
    double res; 
    try 
    { 
     res = db.Stocks.AsNoTracking().Where(c => c.Product.ID == ID && c.WarehouseID == warehouse.ID). 
      AsNoTracking().AsEnumerable(). 
      SkipWhile(c => c.InvoiceItems.Any(q => q.Invoice.IsStocked == false)).Sum(q => q.Quantity); 
    } 
    catch (Exception) 
    { 
     res = 0; 
    } 
    return res; 
} 
+2

請檢查http://stackoverflow.com/questions/2527047/linq-is-skipwhile-broken,您對SkipWhile的期望可能不同 –

回答

1

您可以試試其中條款。

public double GetInStockQuantity(Warehouse warehouse, Entities db) 
    { 
     double res; 
     try 
     { 
      res = db.Stocks.AsNoTracking().Where(c => c.Product.ID == ID &&  c.WarehouseID == warehouse.ID). 
      AsNoTracking().AsEnumerable(). 
      Where(c => c.InvoiceItems.Any(q => q.Invoice.IsStocked == true)).Sum(q => q.Quantity); 
     } 
     catch (Exception) 
     { 
      res = 0; 
     } 
      return res; 
    }