Common Table Expression in EntityFramework

后端 未结 5 1953
小蘑菇
小蘑菇 2020-12-19 05:34

I have this query in Sql Server which I need to consume in EntityFramework, So how can I write a EntityFramwork code which will have the same result as this

         


        
相关标签:
5条回答
  • 2020-12-19 06:00

    Getting input from the other experts over SO, I have come up with my own way to achieve this.

    IEnumerable<StockGroup> sg = dbContext.ExecuteStoreQuery<StockGroup>(
                            @"WITH    q AS
                                        (
                                        SELECT  *
                                        FROM    LedgerGroups
                                        WHERE   GroupParent = 'Customers'
                                        UNION ALL
                                        SELECT  m.*
                                        FROM    LedgerGroups m
                                        JOIN    q
                                        ON      m.GroupParent = q.GroupName
                                        )
                                SELECT  *
                                FROM    q
                            ");
    
    0 讨论(0)
  • 2020-12-19 06:04

    I dont think there is support for recursive CTEs in LINQ nor in EF. The solution is to expose the CTE as a view. The article on Recursive or hierarchical queries using EF Code First and Migrations shows how to deploy such a view using EF code first migrations. Recursive or hierarchical queries using EF Code First and Migrations

    Original source: https://stackoverflow.com/a/11929928/3850405

    0 讨论(0)
  • 2020-12-19 06:08

    EF does not support recursive CTE's. Use a view or a table valued function.

    0 讨论(0)
  • 2020-12-19 06:23

    You cannot use CTE recursion in Entity Framework.

    0 讨论(0)
  • 2020-12-19 06:23

    Use stored procedure and call that stored procedure through EF

    0 讨论(0)
提交回复
热议问题