2

我有这个 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();
4

3 回答 3

6

像这样的东西:

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

?: 语法称为条件运算符。

于 2013-06-13T18:38:26.923 回答
0

我不会添加另一个join,而是在 a 中使用单独的查询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 将如何处理这样的查询(如果它处理它的话)。

于 2013-06-13T19:09:12.160 回答
-1
var query= from x in context.a 
       where String.IsNullOrEmpty(param1) || (x.p == param1 && x.i == param2)
        select x;
于 2013-06-13T18:39:22.000 回答