我有一个名为Comment的类,它有一个属性,是一个注释列表。所以评论可以有多个回复。例如:
comments = new List<Comment>
{
new Comment
{
CommentText = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.",
ParentBlogId = blogId,
PublishedDate = DateTime.Now,
User = new User{UserName = "arnvanhoutte"},
Replies = new List<Comment>
{
new Comment
{
CommentText = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.",
ParentBlogId = blogId,
PublishedDate = DateTime.Now,
User = new User{UserName = "arnvanhoutte"},
Replies = new List<Comment>
{
new Comment
{
CommentText = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.",
ParentBlogId = blogId,
PublishedDate = DateTime.Now,
User = new User{UserName = "arnvanhoutte"}
}
}
},
new Comment
{
CommentText = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.",
ParentBlogId = blogId,
PublishedDate = DateTime.Now,
User = new User{UserName = "arnvanhoutte"}
}
}
},
new Comment
{
CommentText = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.",
ParentBlogId = blogId,
PublishedDate = DateTime.Now,
User = new User{UserName = "arnvanhoutte"}
},
new Comment
{
CommentText = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.",
ParentBlogId = blogId,
PublishedDate = DateTime.Now,
User = new User{UserName = "arnvanhoutte"}
}
};
现在我想知道这个列表中有多少条评论。所以如果我做comments.Count()
我只会得到3.但是在该列表中实际上有6条评论。我怎么算这些呢?
答案 0 :(得分:2)
这是一个递归函数,可以计算所有注释:
public int RecursiveCount(List<Comment> commentList)
{
int partialCount = commentList.Count();
foreach (Comment c in commentList)
if(c.Replies != null)
partialCount += RecursiveCount(c.Replies);
return partialCount;
}