簡體   English   中英

我如何在linq中的select語句中使用where子句。 (即在某個物業上。)

[英]How can i use a where clause within a select statement in linq. (ie. on a property.)

我有這個linq查詢:

var sku = (from a in con.MagentoStockBalances
           join b in con.MFGParts on a.SKU equals b.mfgPartKey
           join c in con.DCInventory_Currents on b.mfgPartKey equals c.mfgPartKey
           where a.SKU != 0 && c.dcKey ==6
           select new
           {
               Part_Number = b.mfgPartNumber,
               Stock = a.stockBalance,
               Recomended = a.RecomendedStock,
               Cato = c.totalOnHandQuantity
           }).ToList();

現在我需要刪除c.dcKey == 6條件,並進行如下操作:

var sku = (from a in con.MagentoStockBalances
           join b in con.MFGParts on a.SKU equals b.mfgPartKey
           join c in con.DCInventory_Currents on b.mfgPartKey equals c.mfgPartKey
           where a.SKU != 0
           select new
           {
               Part_Number = b.mfgPartNumber,
               Stock = a.stockBalance,
               Recomended = a.RecomendedStock,
               Cato = c.totalOnHandQuantity where c.dcKey == 6,
              Kerry = c.totalOnHandQuantity where c.dcKey == 7
           }).ToList();

像這樣:

Cato = c.dcKey == 6 ? c.totalOnHandQuantity : 0,
Kerry = c.dcKey == 7 ? c.totalOnHandQuantity : 0

?:語法稱為條件運算符。

與其添加另一個join ,不如在let使用單獨的查詢:

from a in con.MagentoStockBalances
join b in con.MFGParts on a.SKU equals b.mfgPartKey
where a.SKU != 0
let cs = con.DCInventory_Currents.Where(c => b.mfgPartKey == c.mfgPartKey)
select new
{
    Part_Number = b.mfgPartNumber,
    Stock = a.stockBalance,
    Recomended = a.RecomendedStock,
    Cato = cs.Single(c => c.dcKey == 6).totalOnHandQuantity 
    Kerry = cs.Single(c => c.dcKey == 7).totalOnHandQuantity 
}

盡管我不知道LINQ to SQL處理這樣的查詢的性能如何(如果可以處理的話)。

var query= from x in context.a 
       where String.IsNullOrEmpty(param1) || (x.p == param1 && x.i == param2)
        select x;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM