我有一个使用 CTE 编写的 SQL 查询。现在,我正在移动存储库以使用 Entity Framework 5。
我不知道如何使用 Entity Framework 5 集成(或重写)基于 CTE 的查询。
我正在使用带有 EF5 的 POCO 实体,并且有一堆 Map 类。没有 EDMX 文件等。
我现在感觉自己像个菜鸟,如果有任何帮助指出我正确的方向,我将不胜感激。
CTE查询如下
WITH CDE AS
(
SELECT * FROM collaboration.Workspace AS W WHERE W.Id = @WorkspaceId
UNION ALL
SELECT W.* FROM collaboration.Workspace AS W INNER JOIN CDE ON W.ParentId = CDE.Id AND W.ParentId <> '00000000-0000-0000-0000-000000000000'
)
SELECT
W.Id AS Id,
W.Name AS Name,
W.Description AS Description,
MAX(WH.ActionedTimeUtc) AS LastUpdatedTimeUtc,
WH.ActorId AS LastUpdateUserId
FROM
collaboration.Workspace AS W
INNER JOIN
collaboration.WorkspaceHistory AS WH ON W.Id = WH.WorkspaceId
INNER JOIN
(
SELECT TOP 10
CDE.Id
FROM
CDE
INNER JOIN
collaboration.WorkspaceHistory AS WH ON WH.WorkspaceId = CDE.Id
WHERE
CDE.Id <> @WorkspaceId
GROUP BY
CDE.Id,
CDE.ParentId,
WH.ActorId,
WH.Action
HAVING
WH.ActorId = @UserId
AND
WH.Action <> 4
ORDER BY
COUNT(*) DESC
) AS Q ON Q.Id = WH.WorkspaceId
GROUP BY
W.Id,
W.Name,
W.Description,
WH.ActorId
HAVING
WH.ActorId = @UserId