【发布时间】:2012-11-12 04:02:03
【问题描述】:
我在 Sql Server 中有这个查询,我需要在 EntityFramework 中使用它,那么我该如何编写一个与此结果相同的 EntityFramwork 代码
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
我不知道如何在EF中转换,所以我尝试了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
}
但结果不一样,和 CTE 一样递归。
【问题讨论】:
标签: c# .net linq entity-framework-4 common-table-expression