0

我有这个评论模型,每个评论都可以是其他评论的答案。

 public class Comment
 {
    public virtual int Id { get; set; }
    public virtual string Body { get; set; }
    public virtual DateTime? AddedDate { get; set; }
    public virtual bool IsApproved { get; set; }
    public virtual int LikeCount { get; set; }
    public virtual User User { get; set; }
    public virtual AnonymousUser AnonymousUser { get; set; }
    public virtual Post Post { get; set; }
    public virtual Page Page { get; set; }
    public virtual int? ParentId { get; set; }
    public virtual Comment Parent { get; set; }
    public ICollection<Comment> Children { get; set; }
    public virtual byte[] RowVersion { get; set; }
 }

我尝试使用此代码来获取“IsApproved==true”的评论

this._comments
            .Where(comment => comment.Post.Id == postId).Include(comment => comment.User)
            .Include(comment => comment.AnonymousUser)
            .Include(comment => comment.Children).Where(comment => comment.IsApproved == true)
            .OrderBy(comment => comment.AddedDate)
            .Skip(page * count)
            .Take(count).ToList()
            .Where(comment => comment.Parent == null).ToList();

但是这个查询只返回“IsApproved==true”的注释。

我应该怎么做才能过滤所有评论,甚至是孩子。

谢谢

4

2 回答 2

0

通过子项过滤时尝试 Any。

this._comments
            .Where(comment => comment.Post.Id == postId).Include(comment => comment.User)
            .Include(comment => comment.AnnonymousUser)
            .Where(comment => comment.Children.Any(child=>child.IsApproved == true))
            .OrderBy(comment => comment.AddedDate)
            .Skip(page * count)
            .Take(count).ToList()
            .Where(comment => comment.Parent == null).ToList();
于 2013-02-22T14:27:00.597 回答
0

由于.Where(comment => comment.Parent == null).

但总的来说...

如果我对您的理解正确,您希望在平面列表中获得所有已批准且在特定帖子下的评论(包括子评论),对吗?

假设:每条评论都填充了帖子 - 甚至是儿童。

this._comments
        .Include(comment => comment.User)
        .Include(comment => comment.AnnonymousUser)
        .Include(comment => comment.Children)
        .Where(comment => comment.IsApproved && comment.Post.Id == postId)
        .OrderBy(comment => comment.AddedDate)
        .Skip(page * count)
        .Take(count)
        .ToList();

编辑:

或者,如果您只想过滤已批准的子项,请查看这篇文章http://msdn.microsoft.com/en-us/data/jj574232.aspx ,在显式加载相关实体时应用过滤器部分

于 2013-02-22T14:40:30.913 回答