【问题标题】:Implementing comment list deletion to delete all replies, how to?实现评论列表删除以删除所有回复,如何?
【发布时间】:2021-04-18 03:46:26
【问题描述】:

我有一个 C# 中的 ASP.NET MVC 应用程序,它是一个博客。在此用户可以发布 cmets,并且可以在帖子和评论本身上创建 cmets。这意味着我的评论类必须是递归的,这就是我所拥有的:

public class SubComment
{
        [Key]
        public int subCommentId { get; set; }
        public string message { get; set; }
        public DateTime time_posted { get; set; }
        public int parent_comment_id { get; set; }
        public bool read_by_parent { get; set; }
        public int parentFounder_comment_id { get; set; }
        public bool parentIsSub { get; set; }
        public virtual List<SubComment> SubComments { get; set; }

        [ForeignKey("customer")]
        public string UserId { get; set; }
        public Customer customer { get; set; }
}

现在我遇到的问题是,删除评论时,还必须先删除任何嵌套的 cmets。由于每个评论都有一个 cmets 列表,因此可以继续进行下去。我无法考虑如何实施此删除的解决方案。

到目前为止,我可以找到请求删除的评论,并且可以找到对此特定评论的所有回复,但我需要的是所有嵌套的 cmets,以便回复回复等。

这是我的相关操作代码:

  SubComment subComment = context.SubComments.Find(id);

  replies = context.SubComments
                   .Where(sc => sc.parent_comment_id == subComment.subCommentId && sc.parentIsSub == true)
                   .OrderByDescending(sc => sc.time_posted).ToList();

任何人都可以帮助我实现这一点,任何答案表示赞赏。

(我上传了一个类似的问题,我无法删除但它已关闭,所以它不能也没有答案,很遗憾)。

【问题讨论】:

    标签: c# asp.net-mvc nested-lists recursive-datastructures


    【解决方案1】:

    您可以在您的 SubComment 类上创建一个方法。在此方法中,您可以遍历 SubComment 列表(并递归调用相同的方法)。当 SubComment 没有子评论时,可以删除该 Subcomment。

    private void RemoveComment(context as DBContext)
    {
        if (SubComments.Count > 0)
        {
            SubComment sc;
            foreach (sC in SubComments)
            {
                sC.RemoveComment(context)
            }
        }
        else
        {
            //your code to remove element
        }
     }
    

    【讨论】:

    • 您好,感谢您的回答。因此此解决方案将成功删除父评论及其所有回复,但评论的回复可能有自己的回复,不会被删除。这些进一步的回复可以有自己的回复,我想在删除评论本身之前将它们全部删除。因此,如果我有一个列表,其中每个列表都有自己的子列表,我希望所有从父评论或在父评论下方建立的子列表都消失。
    • 我知道这个问题的措辞很糟糕。
    • 是的,它会删除内部的 cmets,它会递归执行,因为它循环遍历所有子项。您需要调用此代码,然后只删除父级。所有的孩子都会离开
    • 对您的代码进行了一些更改并使其正常工作,谢谢,朋友。
    【解决方案2】:

    这是我为澄清而评论的最后一段代码。唯一需要注意的是,我添加到名为 subReplies 的列表的位置实际上应该是删除,但我的特定程序要求我以降序方式删除,所以我将其存储在一个列表中,稍后我会对其进行重组并对每个语句执行一个删除他们都。

    这里是使用 Aldert 的帮助:

      //this method will take in a comment that the user is requesting for deletion and runs on the post action of delete comment
        //confirm. As the class of sub comments is recursive this must loop on itself to locate all nested lists
        private void RemoveComment(SubComment subComment)
        {
            if (subComment.SubComments.Count > 0) //if the comment we want to delete has replies 
            {
                foreach (SubComment sC in subComment.SubComments) //for each reply 
                {
                    if(sC.SubComments.Any()) //if the reply has its own replies 
                    {
                        foreach(SubComment child_sC in sC.SubComments) //for each sub nested reply
                        {
                            RemoveComment(child_sC); //recall this method so that this nested reply becomes the parent in a manner
                        }
                    }
                    subReplies.Add(sC); //as we cannot delete in this order without crashing and causing errors to the database
                    //, therefore, we will store all these comments in a list - store the reply to the comment
                }
            }
            subReplies.Add(subComment); //store the parent comment 
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-03
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多