[英]The best way to order included entities in entity framework?
What is the best practice to order the comment entity?订购评论实体的最佳做法是什么?
public async Task<IList<Post>> GetPosts() {
var posts = _context.Posts
.Include(u => u.User)
.ThenInclude(p => p.Photos)
.Include(c => c.Comments) <---- THIS
.OrderByDescending(p => p.Created)
.ToListAsync();
return await posts;
}
Before you return the posts
, you can order the Comments attached to each post:在您返回
posts
之前,您可以订购附加到每个帖子的评论:
var posts = await _context.Posts
.Include(u => u.User)
.ThenInclude(p => p.Photos)
.Include(c => c.Comments)
.OrderByDescending(p => p.Created)
.ToListAsync();
foreach(var post in posts)
{
post.Comments = post.Comments
.OrderBy(comment => comment.DateCreated)
.ToList();
}
return posts;
I did the ordering above based on a property called DateCreated
.我根据名为
DateCreated
的属性进行了上面的排序。 You have to change this to the comment object property, on which you want to base the ordering.您必须将其更改为您希望基于该属性进行排序的注释对象属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.