dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.82k stars 3.2k forks source link

Query: translate SelectMany-Where-DefaultIfEmpty into LEFT JOIN #7887

Open ghost opened 7 years ago

ghost commented 7 years ago

My query is:

from t1 in Table1
from t2 in Table2.Where(t => t.MyField == t1.MyField).DefaultIsEmpty()
select new
{
    Id = t1.Id
}

EF Core translate this to

SELECT [t1].[Id]
FROM [Table1] AS [t1]
CROSS APPLY (
    SELECT [t0].*
    FROM (
        SELECT NULL AS [empty]
    ) AS [empty0]
    LEFT JOIN (
        SELECT [t20].*
        FROM [Table2AS [t20]
        WHERE [t20].[MyField] = [t1].[MyField]
    ) AS [t0] ON 1 = 1
) AS [t3]
ORDER BY (SELECT 1)
OFFSET @__p_0 ROWS FETCH NEXT @__p_1 ROWS ONLY

while EF6 correctly translates:

SELECT [t0].[Id] AS [Id]
FROM [Table1] AS [t0], [Table2] AS [t1]
WHERE [t1].[MyField] = [t0].[Myfield]

Further technical details

EF Core version: 1.1.1 Database Provider: Microsoft.EntityFrameworkCore.SqlServer Operating system: Windows 10 IDE: Visual Studio 2015 & LinqPad

ajcvickers commented 7 years ago

Putting on the backlog to look at improving the query.

sepehr1014 commented 6 years ago

Any progress on this issue?