c# - 嵌套评论 ef 核心表模型返回所有评论?
问题描述
我有将评论和回复保存在同一张表中的博客文章模型。
public class Comment : Entity, IAggregateRoot
{
private readonly List<Comment> _replies;
public Comment()
{
_replies = new List<Comment>();
}
public string CommentText { get; set; }
public User User { get; set; }
public Post Post { get; set; }
public IReadOnlyList<Comment> Replies => _replies;
}
我正在使用以下 LINQ 查询来检索特定帖子的评论。
public async Task<IEnumerable<Comment>> GetCommentsByPostId(Guid postId)
{
return await Context.Comments
.AsNoTracking()
.Include(c => c.Post)
.Include(c => c.User)
.Include(c => c.Replies)
.Where(c => c.Post.Id == postId)
.ToListAsync();
}
问题是这个查询返回所有评论,我尝试通过添加条件为
.Where(c => c.Post.Id == postId && c.Replies.count() == 0)
但这会返回没有回复的回复,
我需要从数据库中获取嵌套的评论层次结构。
有人可以帮忙吗
谢谢
解决方案
推荐阅读
- rust - rustdiesel-cli 为不同环境设置多个 env 文件
- javascript - 在 Angular 中,如何访问在 iife 中初始化的全局变量?
- qbasic - 如何使用 QBASIC 连接两台电脑进行变量传输
- javascript - 如何让 flexbox 子项填满垂直空间?
- c++ - 基于范围的 for 循环与 auto 说明符结合 static_cast
- python - 类型检查对象参数的正确方法
- mongodb - MongoDB - 更新数组的子文档以包含计算值
- javascript - 全局导入 Nuxt.js 模块
- xcode - lldb 抱怨名为“this”的变量
- c# - C# 任务卡住并且不会做任何事情