Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UseDatabaseNullSemantics still generating NULL checks

Tags:

sql

linq

I have this on my context object (constructor):

this.Configuration.UseDatabaseNullSemantics = true;

But even with this set, this query:

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();

Returns this 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)

It's not hard to see that I'm getting a System.OutOfException error since I have tons of orders with linkedorderid equals NULL.

If I remove OR (([Extent2].[linkedorderid] IS NULL) AND ([Extent3].[linkedorderid] IS NULL)) query runs fine and returns all invoices that have or not children work orders...

Any idea how can I solve this ?

like image 855
Catinodeh Avatar asked Jan 18 '26 07:01

Catinodeh


1 Answers

Does this work? If the posted query is correct when you remove the (or (is null and is null)) clause, then I think this will execute the same query. (This assumes there isn't a logic error in the joins, as suggested in other posts.)

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();
like image 137
Max Szczurek Avatar answered Jan 20 '26 22:01

Max Szczurek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!