gpt4 book ai didi

c# - EntityFramework 中的公用表表达式

转载 作者:太空狗 更新时间:2023-10-29 19:57:14 26 4
gpt4 key购买 nike

我在 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一样递归。

最佳答案

EF 不支持递归 CTE。使用 View 或表值函数。

关于c# - EntityFramework 中的公用表表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13535003/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com