Linq 构建帖子树

发布于 2024-12-28 18:25:44 字数 299 浏览 3 评论 0原文

我的示例代码只深入两层,我希望它继续沿着树向下遍历,直到没有子节点为止。有谁知道我如何使用 linq 表达式来完成此任务

static List<Post> GetPosts(int postID) 
{
    var posts = from post in myEntities.Posts
           where post.PostID == postID || post.ParentPostID == postID
                select post).ToList();
} 

My example code only goes two levels deep and i would like it to keep traversing down the tree until it has no children. does anyone know how i can accomplish this using linq expressions

static List<Post> GetPosts(int postID) 
{
    var posts = from post in myEntities.Posts
           where post.PostID == postID || post.ParentPostID == postID
                select post).ToList();
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你是暖光i 2025-01-04 18:25:44

您无法编写可使用 Linq2Entities 转换为 SQL 的递归 linq 查询。我的建议是将帖子放入内存并遍历它们或使用 常用表表达式

You can not write recursive linq queries that can be translated into SQL with Linq2Entities. My suggestion is to bring the posts up into memory and traverse them or write a recursive SQL query using Common Table Expressions

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文