Translate embedded IQueryables to common table expressions (CTEs)
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.4k
- PR merge metrics
- PR metrics pending
Description
When reusing a separate `IQueryable` in operations, e.g. `.Where(obj => otherQuery.Contains(obj))`, EF Core generates a subquery (as expected). However, in these cases, manually reviewing generated SQL in debug logs becomes more difficult to visually/mentally parse and damages the readability of the code, especially when a given `IQueryable` is injected multiple times and/or the generated SQL is especially large.
I would like to have the ability to request that applicable queries replace all injected subqueries of this nature with common table expressions named after the injected `IQueryable`'s symbol (like `otherQuery` in the previous example). This would in turn make reading the debug logs of executed SQL statements more readable.
This could just be an opt-in optional behavior specifically for use in debug contexts. One could apply this strategy individually via something like `AsNoTracking()` or app-wide via the `DbContextOptionsBuilder` like with `EnableDetailedErrors()`.
Related Issues: #26486 (CTEs via a special LINQ operator, including recursive)
Contributor guide
Assessment
This issue has not been assessed yet.