LINQ to SQL的 - 如何添加一个where子句为左连接?(LINQ to SQL - Ho

2019-10-17 11:53发布

此LINQ查询表达式会发出左的连接和工作原理:

from p in Prices
join ip in ItemPrices 
    on new { p.PriceId,  ItemId = 7 } equals 
       new { ip.PriceId, ip.ItemId }
into priceItemPrice
from pip in priceItemPrice.DefaultIfEmpty()
select new
{
    pricesPriceId = p.PriceId,
    z = (int?)pip.PriceId,
    p.Content,
    p.PriceMinQ
}

SQL发出:

-- Region Parameters
DECLARE @p0 Int = 7
-- EndRegion
SELECT [t0].[priceId] AS [pricesPriceId], 
    [t1].[priceId] AS [z], 
    [t0].[price] AS [Content], 
    [t0].[priceMinQ] AS [PriceMinQ]
FROM [price] AS [t0]
LEFT OUTER JOIN [itemPrice] AS [t1] 
    ON ([t0].[priceId] = [t1].[priceId]) 
    AND (@p0 = [t1].[itemId])

我怎样才能得到它发出下面的SQL? 它只是上涨了最终的where子句。 WHERE子句不下的“从PIP”和DefaultIfEmpty前一个其中lambda表达式()不工作接受。 我知道我可以在选择过滤出来,但是这不是我所需要的。

SELECT [t0].[priceId] AS [pricesPriceId], 
    [t1].[priceId] AS [z], 
    [t0].[price] AS [Content], 
    [t0].[priceMinQ] AS [PriceMinQ]
FROM [price] AS [t0]
LEFT OUTER JOIN [itemPrice] AS [t1] 
    ON ([t0].[priceId] = [t1].[priceId]) 
    AND (@p0 = [t1].[itemId])
WHERE [t1].[priceId] is null

更新 Oy公司合租的,我的错误,where子句做工作-由于某种原因,VS2008并没有表现并给我的悲伤,我的肚子咆哮。 我在LinqPad测试背部和where子句很好。 所以这一点除了没有工作:

...
from pip in priceItemPrice.DefaultIfEmpty()
*** where pip.ItemId == null ***
select new
...

Answer 1:

下面是如何的样本OneDotNetWay做过类似的事情。 我试图把他们的榜样和您的查询相匹配。

var query =  p in Prices
   join ip in ItemPrices 
   on 
   new { p.PriceId,  ItemId = 7 } 
   equals 
   new { ip.PriceId, ip.ItemId }
   into priceItemPrice
   from pip in priceItemPrice.DefaultIfEmpty()
   select new
   {
      pricesPriceId = p.PriceId,
      z = (int?)pip.PriceId,
      p.Content,
      p.PriceMinQ
   }


文章来源: LINQ to SQL - How to add a where clause to a left join?