efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Select uses INNER JOIN instead of LEFT JOIN

Open cbmek opened this issue 10 years ago • 9 comments

The case

Lets say we have two objects with nullable relation one-to-many:

public class Post {
    public int Id { get; set; }
    public string Title{ get; set; }
    public virtual ICollection<Comment> Comments { get; set; }
}
public class Comment {
    public int Id { get; set; }
    public string Content { get; set; }
    public int? PostId {get; set; }
    public virtual Post Post { get; set; }
}

We want to get all the comments with it's Posts (if any).

var all = _context.Comments.Include(m => m.Post).ToList();

it will do a single query, with LEFT JOIN, and results are correct. If Comments is not assigned to Post then Post property will equal null - PERFECT!

The problem

...but let's use Select, like this:

var all = _context.Comments.Include(m => m.Post).Select(m => new { 
    Something = m.Content, 
    PostTitle = Post != null ? Post.Title : "NO POST"  
});

and strange thing happen, the SQL query uses INNER JOIN, so we end *_only *_with Comments witch has Post assigned. In my opinion it should still use LEFT JOIN.

Temp solution

The only way to make it to work like it should now is to do the ToList() before Select():

var all = _context.Comments.Include(m => m.Post).ToList().Select(m => new { 
    Something = m.Content, 
    PostTitle = Post != null ? Post.Title : "NO POST"  
});

cbmek avatar Feb 24 '16 14:02 cbmek

How does the SQL look like if you omit the Include:

var all = _context.Comments.Select(m => new { 
    Something = m.Content, 
    PostTitle = Post != null ? Post.Title : "NO POST"  
});

Is there any difference?

gdoron avatar Feb 24 '16 18:02 gdoron

No, it's still INNER JOIN. And more interesting it that when you do 3 includes but use only one reference in Select, it will do only one INNER JOIN - so Select probably cancel all previous includes.

cbmek avatar Feb 24 '16 18:02 cbmek

@cbmek you can try the following:

from c in _context.Comments
join p in _context.Post on c.PostId equals p.Id into grouping
from p in grouping.DefaultIfEmpty()
select new { Something = p.Content, PostTitle = p != null ? p.Title : "NO POST" }

The actual issue of optional navigation translation to LOJ has been addressed in c4e44471b6f41f96d37b1acfbbb4cc97cd11ee89 and should be available in the next release

maumar avatar Feb 24 '16 19:02 maumar

@cbmek one thing I should add, there is currently no advantage of the DefaultIfEmpty approach (if it even works in released bits) over what you initially did (Include + ToList() ), since currently everything after DefaultIfEmpty will be evaluated on the client, so all properties of Comment and Post will be send to the client. We have an issue that tracks this: #4588

maumar avatar Feb 24 '16 19:02 maumar

@cbmek @gdoron Include doesn't work with optional navigations currently. #4589 tracks this problem

maumar avatar Feb 24 '16 19:02 maumar

This bug really is fatal and only requires an optional FK to reproduce. Please fix this in RC2.

strohhut avatar Mar 03 '16 21:03 strohhut

@strohhut it is already fixed in our current dev branch

maumar avatar Mar 03 '16 22:03 maumar

Same problem here with 3.1. When x => false then the list is empty, but when x => true then the list is field. I have checked sql query and it does an inners join, instead of an left join.

GroupCoachStudents.Select(gcs => new PrelimStudentDto()
{
    Student = gcs.CoachStudentUser.Student,
    DueSession = group.Sessions
        .Where(x => false)
        .FirstOrDefault()
}),

danielcojocaru avatar Nov 28 '20 22:11 danielcojocaru

@danielcojocaru This is a very old closed issues. If you're still having an issue, then please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

ajcvickers avatar Nov 30 '20 21:11 ajcvickers