npgsql / EntityFramework6.Npgsql

Entity Framework 6 provider for PostgreSQL
PostgreSQL License
66 stars 54 forks source link

DefaultIfEmpty mess with OrderBy #56

Open RafaelSalguero opened 7 years ago

RafaelSalguero commented 7 years ago

The following query is returned correctly ordered:

c.Transactions
.OrderByDescending(x => x.Date)
.Select(x => x.Acum)
.ToList();

But this other query not:

c.Transactions
.OrderByDescending(x => x.Date)
.Select(x => x.Acum)
.DefaultIfEmpty()
.ToList();

These are the generated SQLs for both queries, as you can see the second doesn't have an ORDER BY clause:

SELECT "Extent1"."Acum" 
FROM "public"."Transactions" 
AS "Extent1" ORDER BY "Extent1"."Date" DESC  
SELECT  CASE  
WHEN ("Project1"."C1" IS NULL) 
THEN (0::numeric) 
ELSE ("Project1"."Acum") END  AS "C1" 
FROM (SELECT 1 AS "C") AS "SingleRowTable1" 
LEFT OUTER JOIN 
(SELECT "Extent1"."Acum", 1::int2 AS "C1" FROM "public"."Transactions" AS "Extent1") AS "Project1" 
ON TRUE = TRUE
Emill commented 7 years ago

I would guess it's a bug in Entity Framework itself rather than in Npgsql's EF provider. Could you try to do the same when using SQL Server's EF provider?