2017-05-26 75 views
-1

我試圖添加一個布爾值(InStock)到一個Linq查詢,其中它設置一個基於條件的值,但我得到一個錯誤:將一個布爾值添加到'選擇新的'Linq查詢

The name 'Any' does not exist in the current context. Cannot resolve symbol 'Any1'

不確定這裏的語法是什麼?

var query = from o in Orders 
      join i in OrderItems on o.SKU equals i.SKU 
      where o.OrderId == orderId 
      select new OrderObject 
      { 
       Id = o.OrderId, 
       WarehouseQty = i.AvailableQty, 
       Sku = i.Sku, 
       InStock = Any(x => x.AvailableQty > 0) //<--need to assign boolean here 

      }; 
+5

那麼你期待「任何」做什麼?你處於有單*訂單項和單*訂單的環境中。 –

+0

如果'AvailableQty'> 0,則將'InStock'設置爲'true'。 – PixelPaul

+2

Like'InStock = i.AvailableQty> 0'? – DavidG

回答

1

If the AvailableQty is > 0, then set InStock to true

好,然後用

var query = from o in Orders 
      join i in OrderItems on o.SKU equals i.SKU 
      where o.OrderId == orderId 
      select new OrderObject 
      { 
       Id = o.OrderId, 
       WarehouseQty = i.AvailableQty, 
       Sku = i.Sku, 
       InStock = i.AvailableQty > 0 
      }; 

無需使用Enumerable.Any這是用來搜索其他收藏品。

+0

呃!謝謝。 – PixelPaul

1

你可能想使用InStock = i.AvailableQty > 0,像這樣:

var query = from o in Orders 
      join i in OrderItems on o.SKU equals i.SKU 
      where o.OrderId == orderId 
      select new OrderObject 
      { 
       Id = o.OrderId, 
       WarehouseQty = i.AvailableQty, 
       Sku = i.Sku, 
       InStock = i.AvailableQty > 0 
      }; 

但是,它似乎是多餘的具有這種WarehouseQty財產。爲什麼不只是添加一個只讀的屬性到你的班級:

public class OrderObject 
{ 
    //snip 

    public bool InStock => WarehouseQty > 0; 
}