Assuming you have a Post DTO / View model / POCO like this
public class PostDto
{
public string Title{ set; get; }
public int Id { set; get; }
public List<PostDto> Comments { set; get; }
public int TotalCommentCount { set; get; }
}
5 .
OrderByDescending, , (, ..)
var posts = dbContext.Posts.
Select(s => new PostDto
{
Id = s.PostId,
Title= s.PostTitle,
TotalCommentCount = s.Comments.Count(),
Comments = s.Comments.OrderByDescending(f => f.Id).Take(5)
.Select(x => new PostDto
{
Id = x.CommentId,
Name = x.CommentText
}).ToList()
}).ToList();
db.