linq 左连接生成的sql带有or条件。看代码

AlenOU 2017-12-07 10:40:38
linq代码:
var stockQuery = from od in db.sto_StockInOrderDetail.Where(ox => ox.DetailType == 2)
join o in db.sto_StockInOrder.Where(x => x.BusinessBillID == "305" && x.State == 200) on od.StockInID equals o.StockInID
select new { ProductID =od.ProductID, StockInID =o.StockInID, SupplierID =o.SupplierID};

var querydata = from q in query.Where(xq=>xq.ProductID !="")
join b in stockQuery.Distinct() on q.ProductID equals b.ProductID
into temp
from tempD in temp.DefaultIfEmpty()
join s in db.core_Good on q.SKU equals s.SKU
select new { record = q,good = s,sup = tempD.SupplierID, PurchaseCode = tempD.StockInID };

跟踪出来的sql:


怎么会多个or条件呢?有这个。导致sql查询很慢。去掉这个or,查询就很快出来的!
求大神指点,怎么让左连接不生成这个or条件。
...全文
628 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
正怒月神 2017-12-07
  • 打赏
  • 举报
回复
如果你要去除null的product。那么就使用//.Where(xq=>xq.ProductID !=""&&xq.ProductID!=null)
正怒月神 2017-12-07
  • 打赏
  • 举报
回复
引用 6 楼 oubocheng 的回复:
这样写不就是inner join 全连接了么?我有个是左连接的!
哦,我以为以你第二段代码为准呢。 linq左联就是通过查询 null来达到左联的。 而第二段代码,我估计是2个form引起的。
AlenOU 2017-12-07
  • 打赏
  • 举报
回复
引用 5 楼 hanjun0612 的回复:
var querydata = from q in query join b in stockQuery.Distinct() on q.ProductID equals b.ProductID join s in db.core_Good on q.SKU equals s.SKU select new { 。。。。 }; 这样呢?
这样写不就是inner join 全连接了么?我有个是左连接的!
正怒月神 2017-12-07
  • 打赏
  • 举报
回复
var querydata = from q in query join b in stockQuery.Distinct() on q.ProductID equals b.ProductID join s in db.core_Good on q.SKU equals s.SKU select new { 。。。。 }; 这样呢?
AlenOU 2017-12-07
  • 打赏
  • 举报
回复
引用 3 楼 hanjun0612 的回复:
[quote=引用 2 楼 oubocheng 的回复:] [quote=引用 1 楼 hanjun0612 的回复:] 因为 from tempD in temp.DefaultIfEmpty() 就是查询为空的啊。
我去掉那个DefaultIfEmpty(),跟出来的sql,还是有or条件[/quote] 你去掉的代码和sql发出来看一下[/quote]

var querydata = from q in query//.Where(xq=>xq.ProductID !="")
                                join b in stockQuery.Distinct() on q.ProductID equals b.ProductID
                                into temp
                                from tempD in temp
                                join s in db.core_Good on q.SKU equals s.SKU
                                select new { record = q,good = s,sup = tempD.SupplierID, PurchaseCode = tempD.StockInID };
跟踪出来的sql

SELECT 
    ....
    FROM   [dbo].[sto_StockRecord] AS [Extent1]
    INNER JOIN  (SELECT DISTINCT 
        [Extent2].[ProductID] AS [ProductID], 
        [Extent3].[StockInID] AS [StockInID], 
        [Extent3].[SupplierID] AS [SupplierID]
        FROM  [dbo].[sto_StockInOrderDetail] AS [Extent2]
        INNER JOIN [dbo].[sto_StockInOrder] AS [Extent3] ON [Extent2].[StockInID] = [Extent3].[StockInID]
        WHERE (2 = [Extent2].[DetailType]) AND (N'305' = [Extent3].[BusinessBillID]) AND (200 = [Extent3].[State]) ) AS [Distinct1] 
        ON ([Extent1].[ProductID] = [Distinct1].[ProductID]) OR (([Extent1].[ProductID] IS NULL) AND ([Distinct1].[ProductID] IS NULL))
    INNER JOIN [dbo].[core_Good] AS [Extent4] ON [Extent1].[SKU] = [Extent4].[SKU]
正怒月神 2017-12-07
  • 打赏
  • 举报
回复
引用 2 楼 oubocheng 的回复:
[quote=引用 1 楼 hanjun0612 的回复:] 因为 from tempD in temp.DefaultIfEmpty() 就是查询为空的啊。
我去掉那个DefaultIfEmpty(),跟出来的sql,还是有or条件[/quote] 你去掉的代码和sql发出来看一下
AlenOU 2017-12-07
  • 打赏
  • 举报
回复
引用 1 楼 hanjun0612 的回复:
因为 from tempD in temp.DefaultIfEmpty() 就是查询为空的啊。
我去掉那个DefaultIfEmpty(),跟出来的sql,还是有or条件
正怒月神 2017-12-07
  • 打赏
  • 举报
回复
因为 from tempD in temp.DefaultIfEmpty() 就是查询为空的啊。

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧