实现评论列表删除删除所有回复,怎么做?
Implementing comment list deletion to delete all replies, how to?
我在 C# 中有一个 ASP.NET MVC 应用程序,它是一个博客。在此用户可以 post 发表评论,并且可以对 post 和评论本身发表评论。这意味着我的评论 class 必须是递归的,这就是我所拥有的:
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; }
}
现在我遇到的问题是,当删除评论时,还必须先删除任何嵌套的评论。由于每个评论都有一个评论列表,因此可以继续下去。关于如何实施此删除,我无法想出解决方案。
到目前为止,我可以找到请求删除的评论,并且可以找到对该特定评论的所有回复,但我需要的是所有嵌套评论,以便回复回复等。
这是我的相关操作代码:
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();
任何人都可以帮助我实现这个,任何答案都将不胜感激。
(我上传了一个类似的问题,我无法删除但它已关闭,所以它不能也没有答案,不幸的是)。
您可以在 SubComment 上创建一个方法 class.In 此方法循环遍历 SubComments 列表(并递归调用相同的方法)。当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
}
}
这是我评论过的最后一段代码,以供澄清。唯一要注意的是我添加到一个名为 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
}
我在 C# 中有一个 ASP.NET MVC 应用程序,它是一个博客。在此用户可以 post 发表评论,并且可以对 post 和评论本身发表评论。这意味着我的评论 class 必须是递归的,这就是我所拥有的:
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; }
}
现在我遇到的问题是,当删除评论时,还必须先删除任何嵌套的评论。由于每个评论都有一个评论列表,因此可以继续下去。关于如何实施此删除,我无法想出解决方案。
到目前为止,我可以找到请求删除的评论,并且可以找到对该特定评论的所有回复,但我需要的是所有嵌套评论,以便回复回复等。
这是我的相关操作代码:
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();
任何人都可以帮助我实现这个,任何答案都将不胜感激。
(我上传了一个类似的问题,我无法删除但它已关闭,所以它不能也没有答案,不幸的是)。
您可以在 SubComment 上创建一个方法 class.In 此方法循环遍历 SubComments 列表(并递归调用相同的方法)。当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
}
}
这是我评论过的最后一段代码,以供澄清。唯一要注意的是我添加到一个名为 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
}