Skip to content

Support for common table expressions (aka WITH) via special LINQ operator #26486

Description

@roji

We could have a special queryable-returning function which we'd translate into a TableExpressionBase:

from foo in EF.With(ctx.Foos.Where(....))
from bar in EF.With(ctx.Bars.Where(....))
from baz in ctx.Bazes.Where(...)
...

... becomes:

WITH foo AS (
    SELECT * FROM Foos
    WHERE ...
), bar AS (
    SELECT * FROM Bars
    WHERE ...
),
SELECT * FROM Bazes WHERE ...

Do recursive CTEs as well.

Note: CTEs are also valid with WITH ... INSERT ... INTO, WITH ... DELETE, WITH ... UPDATE.

See also #29918 (embedded IQueryables to common table expressions)

Community implementations

Activity

  1. changed the title [-]Support for common table expressions (aka WITH)[/-] [+]Support for common table expressions (aka WITH) using LINQ[/+] on Oct 28, 2021
  2. changed the title [-]Support for common table expressions (aka WITH) using LINQ[/-] [+]Support for common table expressions (aka WITH) by translating LINQ query[/+] on Oct 28, 2021
  3. added this to the Backlog milestone on Nov 2, 2021
  4. roji commented on Dec 19, 2021

    @roji
    MemberAuthor

    Note: all major databases support prefixing UPDATE/DELETE with CTEs (see #795 for bulk updates).

  5. changed the title [-]Support for common table expressions (aka WITH) by translating LINQ query[/-] [+]Support for common table expressions (aka WITH) via special LINQ operator[/+] on Apr 25, 2023
  6. roji commented on Aug 27, 2023

    @roji
    MemberAuthor

    Note: all databases - except SQL Server - support embedding CTEs within arbitrary subqueries inside the query;

    SELECT * FROM (WITH t AS (SELECT 1) SELECT * FROM t) AS foo;

    This effectively makes CTEs an optional prefix of SelectExpression.

    In addition, SQL Server (but not other databases) requires that column names be explicitly specified when they're not named within the subquery:

    WITH t AS (SELECT 1 AS q) SELECT * FROM t; -- Column name bubbles out of the subquery
    WITH t (q) AS (SELECT 1) SELECT * FROM t; -- Column explicitly specified as part of the CTE syntax
    WITH t AS (SELECT 1) SELECT * FROM t; -- Fails in SQL Server, works elsewhere (with unknown column name)
  7. mojtabakaviani commented on Nov 23, 2024

    @mojtabakaviani

    @ajcvickers and @roji
    I hope CTEs and window functions #12747 will be added to 10.0 milestone for boost performance of advanced queries with grouping and aggregation.

  8. Pepsi1x1 commented on Dec 6, 2024

    @Pepsi1x1

    +1 this would be great to see and would benefit us across a number of projects

  9. added theissue type on Jun 15, 2025
  10. marked Common Table Expression (CTE) #30757 as a duplicate of this issue on Jun 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions