Tag: common table expression

Linq-to-Sql:递归获取子项

我有一个Comment表,它有一个CommentID和一个ParentCommentID。 我想获得评论中所有孩子的清单。 这是我到目前为止,我还没有测试过。 private List searchedCommentIDs = new List(); // searchedCommentIDs is a list of already yielded comments stored // so that malformed data does not result in an infinite loop. public IEnumerable GetReplies(int commentID) { var db = new DataClassesDataContext(); var replies = db.Comments .Where(c => c.ParentCommentID == commentID && !searchedCommentIDs.Contains(commentID)); foreach (Comment reply […]

EntityFramework中的公用表表达式

我在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中转换它,所以我试着加入。 from a in db.StockGroups join b in db.StockGroups on new { GroupParent = a.GroupParent } equals new { GroupParent = b.GroupName } where […]

在C#中模拟CTE递归

假设有以下CTE返回我所拥有的某些树数据(邻接模型)的级别(取自Linq中的分层数据 – 选项和性能 ): WITH hierarchy_cte(id, parent_id, data, lvl) AS ( SELECT id, parent_id, data, 0 AS lvl FROM dbo.hierarchical_table WHERE (parent_id IS NULL) UNION ALL SELECT t1.id, t1.parent_id, t1.data, h.lvl + 1 AS lvl FROM dbo.hierarchical_table AS t1 INNER JOIN hierarchy_cte AS h ON t1.parent_id = h.id ) SELECT id, parent_id, data, lvl FROM […]