Common Table Expression in Entity Framework

Habe ich diese Abfrage in Sql Server, die ich brauche, um zu konsumieren, in EntityFramework, Also wie kann ich schreiben Sie eine EntityFramwork code, die das gleiche Ergebnis wie diese

WITH    cte AS
        (
        SELECT  *
        FROM    StockGroups
        WHERE   GroupParent ='Stationery' 
        UNION ALL
        SELECT  g.*
        FROM    StockGroups g
        JOIN    cte
        ON      g.GroupParent = cte.GroupName
        )
SELECT  *
FROM    cte

Ich weiß nicht, wie es zu konvertieren in der EF, also versuchte ich mit join.

from a in db.StockGroups
join b in db.StockGroups on new { GroupParent = a.GroupParent } equals new { GroupParent = b.GroupName }
where
  b.GroupName == "Stationery"
select new {
  a.GroupName,
  a.GroupParent,
  Column1 = b.GroupName,
  Column2 = b.GroupParent
}

Aber das Ergebnis ist nicht dasselbe, wie als rekursive CTE.

  • Verwenden Sie mehr linq 😛
Schreibe einen Kommentar