question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to Use Navigation Property generate left join sql not inner join sql

See original GitHub issue

When Use Navigate Property one to one:

Normally the sql is a inner join , I do not know how to write a left Join use one to one Navigate Property

usually,we can also use Include to make ef generate a left join sql ,there is other method?

I donot like linq like this:

var LeftJoin = from a in aList
               join b in bList on a.Id equals b.Id into cc
               from b in cc.DefaultIfEmpty()

can implent like this?

      _context.Doctor.where(t=>t.Id==id)
                    .Select(doctor => new DoctorDTO()
                     {
                         Name=doctor.Name,
                        HospitalName=doctor.Hospital.Name  // want  left join  not innner join
                      }

In project ,i use automapper ProjectTo< DTO >,but how can i use One to One Navigation property generate left join sql?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
rojicommented, Dec 1, 2021

@ByZhouhang AFAIK EF Core does not generate RIGHT JOIN - it would be a bit difficult to imagine the LINQ that would be used to express such a query; you’ll have to start your LINQ query from UserAnswer. Note that RIGHT JOINs are mathematically rewritable as LEFT JOINs. Do you have any particular reason to want to start with UserAnswer rather than TrialQuestion, or is a matter of personal preference?

2reactions
rojicommented, Dec 1, 2021

@ByZhouhang as with your first code sample (Doctor and Hospital), if you make UserAnswer.TrialQuestionId nullable, EF Core will produce a LEFT JOIN, since a UserAnswer might not have a TrialQuestion; the “requiredness” of navigations is determined by the nullability of their foreign keys, and that’s what determines whether an INNER or LEFT join is used.

EF Core doesn’t generate RIGHT JOIN, but these are logically the same as LEFT JOIN.

Read more comments on GitHub >

github_iconTop Results From Across the Web

EF Core Navigation Property Include uses Left Join instead ...
I can see on my SQL Server Profiler a left join rather than an inner join. I tried removing the annotations and going...
Read more >
Join Query In Entity Framework
The above code creates the following SQL Query. The EF efficiently decides INNER JOIN & LEFT JOIN based the navigational property. 1. 2....
Read more >
Complex Query Operators - EF Core
The LINQ Join operator allows you to connect two data sources based on the key selector for each source, generating a tuple of...
Read more >
Don't use Linq's Join. Navigate! | Passion for Coding
Don't use Linq's Join. Navigate! ... One of the greatest benefits of LINQ to SQL and LINQ to Entities is navigation properties that...
Read more >
How to Use Entity Framework Left Join?
Entity Framework Left Join is a database-oriented process that joins the data from two or more tables using a join query.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found