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.

Add an IncludeInner method

See original GitHub issue

The relational database model doesn’t have a concept of mandatory children since ownership is defined by a foreign key in a child relation. As a consequence, in SQL, Include always has to translate to OUTER JOIN because there’s no formal language to configure the association as 1-1..n (certainly not vendor-independent). It’s always 1-0..n. A statement like

context.Parents.Include(p => p.Children)

should, of course, return all parent records, not just the ones with children. Hence the outer join.

There are three reasons why this OUTER JOIN may not always be desired.

  1. It’s not uncommon for parents without children to be invalid or meaningless. Fetching them from the database by plain SQL would always involve an INNER JOIN because there is foreknowledge of the database content. (Which in such cases will be enforced by client-side validation).
  2. Irrespective of database content, developers may want to Include children and at the same time filter only parents with children. Here, INNER JOIN is better than OUTER JOIN + a predicate. Again, in plain SQL that would be a no-brainer.
  3. INNER JOINs may perform significantly better than OUTER JOINs, esp. in more complex queries, it would be useful for developers to have control over the type of join EF will execute.

So I wonder if there is a case for adding an IncludeInner (+ ThenIncludeInner) method that does exactly that: always translate to an inner join?

This will also work with filtered includes. It can also be made to work with split queries: one query without results for a parent will remove the parent from the end result.

It is expected from developers to have sufficient knowledge of SQL to understand how INNER and OUTER joins work an how they affect one another, esp. how one INNER JOIN can turn previous OUTER JOINs into de-facto INNER joins.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:6
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
TrobinsonCincomcommented, Aug 19, 2021

I have to upvote the original suggestion and disagree with the idea that optional/required metadata should decide whether inner or outer joins should be used. You don’t define foreign keys as an “inner foreign keys” or “outer foreign keys”. Foreign keys are supposed to allow you to write both inner and outer joins depending on the scenario. Currently you have to write complex LINQ queries to get an inner join on a many to many relationship. Whereas if you were writing raw SQL you would just have to replace a single keyword with LEFT or INNER.

2reactions
smitpatelcommented, May 23, 2021

Instead of going into pattern matching, we could have metadata API to configure that collection will always have 1 child. Just like how we have API to market that principal to dependent reference navigation is required. (i.e. Child will always exist for the given principal). This will be useful for any nav expansion not just include.

Apart from that, users are free to write LINQ when their joins contain additional information not configured in metadata manually using existing LINQ operators to generate inner/outer join as needed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Entity Framework - Include Multiple Levels of Properties
The Include() method works quite well for Lists on objects. But what if I need to go two levels deep? For example, the...
Read more >
Python Inner Functions: What Are They Good For?
In this step-by-step tutorial, you'll learn what inner functions are in Python, how to define them, and what their main use cases are....
Read more >
Method within method in java
Method within method in java · Method 1 (Using anonymous subclasses) It is an inner class without a name and for which only...
Read more >
Nested Classes - Learning the Java Language
As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to...
Read more >
include directive (C/C++)
You can organize constant and macro definitions into include files (also known as header files) and then use #include directives to add them ......
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