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.

Eager loading seems to not work with recursive CTEs?

See original GitHub issue

Is it possible to use .Include/.ThenInclude, aka eager loading, for recursive CTEs? It seems that applying them on the query does absolutely nothing. Is it intended?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sdanylivcommented, Jun 30, 2021

Why not 😉 If you start thinking from SQL query but not from object collections perspective - it is natural way. And surprise, you will get almost the same SQL as a LINQ query.

1reaction
sdanylivcommented, Jun 30, 2021

I have corrected your query and it should work. Currently recursive CTE cannot return whole object but only projection.

class DivisionCte
{
    public int DivisionId;
    public int ParentId;
}
var divisionCte = _context.GetCte<DivisionCte>(d => 
    (
        from div in _context.Divisions
        where div.DivisionId == divId
        select new DivisionCte
        { 
            DivisionId = div.DivisionId
            ParentId = div.ParentId
        }
    )
    .Concat
    (
        from cdiv in _context.Divisions
        join pdiv in d on cdiv.ParentId equals pdiv.DivisionId
        select new DivisionCte
        {
            DivisionId = cdiv.DivisionId,
            ParentId = cdiv.ParentId
        }
    ));

var query = 
    from d in _context.Divisions.Include(d => d.Employees)
    join cte in divisionCte on d.DivisionId equals cte.DivisionId
    select d;

var result = await query.ToArrayAsyncLinqToDB();
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to do recursive load with Entity framework?
Answer: Unless you are using specific hierarchy features of your database server (which are not SQL standard, but supported by some servers, ...
Read more >
Why doesn't this recursive CTE with a parameter use an ...
SQL Server rewrites tail-recursive common table expressions (CTEs) as iteration. Everything from the Lazy Index Spool down is the runtime ...
Read more >
Why is my CTE so slow?
This is referred to as a recursive common table expression. ... Maybe you have written or debugged a CTE that seems to run...
Read more >
Work with recursive CTEs | BigQuery
A non-recursive CTE can reference only preceding CTEs and can't reference itself. Recursive CTEs run continuously until no new results are found, while...
Read more >
WITH common_table_expression (Transact-SQL)
The following guidelines apply to defining a recursive common table expression: The recursive CTE definition must contain at least two CTE 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