LINQ to SQL-如何为左键添加where子句?[英] LINQ to SQL - How to add a where clause to a left join?

本文是小编为大家收集整理的关于LINQ to SQL-如何为左键添加where子句?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

此 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 子句.在"from pip"下不接受 where 子句,并且 DefaultIfEmpty() 之前的 where 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 vey,我的错误,where 子句确实起作用了——出于某种原因,VS2008 没有表现出来,让我很伤心,我的胃在咆哮.我在 LinqPad 中进行了测试,where 子句很好.所以这个小小的补充确实奏效了:

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

推荐答案

这里是一个示例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
   }

本文地址:https://www.itbaoku.cn/post/1557121.html

问题描述

This LINQ query expression emits a left join and works:

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 emitted:

-- 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])

How can I get it to emit the SQL below? It just has the where clause tacked on the end. A where clause is not accepted under the "from pip" and a where lambda expression before DefaultIfEmpty() doesn't work. I know I can filter it out in the select, but that's not what I need.

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

Update Oy vey, my mistake, the where clause did work - for some reason VS2008 was not behaving and giving me grief and my stomach was growling. I tested back in LinqPad and the where clause was fine. So this little addition did work:

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

推荐答案

Here is a sample of how OneDotNetWay has done something similar. I've tried to take their example and match up your query.

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
   }