Linq 构建帖子树
我的示例代码只深入两层,我希望它继续沿着树向下遍历,直到没有子节点为止。有谁知道我如何使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法编写可使用 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