public async Task <ActionResult <IEnumerable <CommentAggregationDTO> > > GetCommentsByPage(int articleId, int limit, int pageIndex) { List <Comment> comments = (await commentService.GetCommentsOnNotReplyAsyncByPageAsync(articleId, limit, pageIndex)).ToList(); List <CommentAggregationDTO> commentsDTOs = new List <CommentAggregationDTO>(); foreach (var item in comments) { CommentAggregationDTO commentAggregationDTO = new CommentAggregationDTO();//父元素 User user = await userService.GetUserByIdAsync(item.UserId); CommentDTO commentDTO = await commentMapper.MapperToSingleDTOAsync(item, user); //递归查询子元素 commentService.ClearRecursionComments(); List <Comment> commentsChildren = commentService.GetCommentsByRecursion(item.CommentId); List <CommentDTO> commentChildrenDTOs = new List <CommentDTO>();//子集元素 foreach (var that in commentsChildren) { User userChildren = await userService.GetUserByIdAsync(that.UserId); CommentDTO commentChildrenDTO = await commentMapper.MapperToSingleDTOAsync(that, userChildren); commentChildrenDTO.TargetUserName = (userService.GetUserByIdAsync((Guid)that.TargetUserId)).Result.UserName; commentChildrenDTOs.Add(commentChildrenDTO); } //汇总 commentAggregationDTO.CommentChildren = commentChildrenDTOs.OrderBy(a => a.CommentDate).ToList(); commentAggregationDTO.CommentParent = commentDTO; commentsDTOs.Add(commentAggregationDTO); } //获取总数 int count = (await commentService.GetCommentsOnNotReplyAsync(articleId)).Count(); return(Ok(new { data = commentsDTOs, total = count })); }