I have this query on Sql Server which I need to consume in EntityFramework. So how can I write EntityFramwork code that will have the same result as this
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
I don't know how to convert it to EF, so I tried with 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 }
But the result is not as recursive as CTE.
Updated:
IEnumerable<StockGroup> sg = dbContext.ExecuteStoreQuery<StockGroup>(@"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 ");
This is how I use EF right now.
Kishore Kumar Nov 23 '12 at 19:52 2012-11-23 19:52
source share