小编典典

UseDatabaseNullSemantics仍会生成NULL检查

sql

我的上下文对象(构造函数)上有这个:

this.Configuration.UseDatabaseNullSemantics = true;

但是即使有这个设置,这个查询:

var query = (from i in _repo.Invoices
             join o in _repo.Orders on i.orderid equals o.orderid
             join o2 in _repo.Orders on o.linkedorderid equals o2.linkedorderid  into leftOrders
             from lo in leftOrders.DefaultIfEmpty()
             where invoiceIds.Contains(i.invoiceid)
             select new
             {
                i, lo
             }).ToList();

返回此SQL:

SELECT 
    [Extent1].[invoiceid] AS [invoiceid], 
    [Extent1].[custid] AS [custid], 
    [Extent1].[orderid] AS [orderid], 
    [Extent1].[orderamount] AS [orderamount], 
    [Extent1].[invoiceamount] AS [invoiceamount], 
    [Extent1].[paidamount] AS [paidamount], 
    [Extent1].[paidstatus] AS [paidstatus], 
    [Extent1].[printdate] AS [printdate], 
    [Extent1].[updateddate] AS [updateddate]
    FROM   [dbo].[invoices] AS [Extent1]
    INNER JOIN [dbo].[orders] AS [Extent2] ON [Extent1].[orderid] = [Extent2].[orderid]
    LEFT JOIN [dbo].[orders] AS [Extent3] ON ([Extent2].[linkedorderid] = [Extent3].[linkedorderid]) OR (([Extent2].[linkedorderid] IS NULL) AND ([Extent3].[linkedorderid] IS NULL))
    WHERE [Extent1].[invoiceid] IN (3098489, 3123185, 3156838)

不难看出我收到了System.OutOfException错误,因为我有成千上万的具有linkedorderid等于NULL的订单。

如果我删除OR (([Extent2].[linkedorderid] IS NULL) AND ([Extent3].[linkedorderid] IS NULL))查询,可以正常运行并返回所有有或没有子工作订单的发票…

知道我该如何解决吗?


阅读 272

收藏
2021-04-07

共1个答案

小编典典

这行得通吗?如果删除(或(null并且为null))子句时发布的查询正确,那么我认为这将执行相同的查询。(这假定联接中没有逻辑错误,如其他文章中所建议。)

var query = (from i in _repo.Invoices
    from o in _repo.Orders.Where(c => c.orderid == i.orderid)
    from o2 in _repo.Orders.Where(c => c.linkedorderid == o.linkedorderid).DefaultIfEmpty()                          
    where invoiceIds.Contains(i.invoiceid)
    select new
    {
       i, o2
    }).ToList();
2021-04-07