在 entity framework 中订购包含实体的最佳方式?
The best way to order included entities in entity framework?
订购评论实体的最佳做法是什么?
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;
}
在 return posts
之前,您可以订购附在每个 post 上的评论:
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;
我根据名为 DateCreated
的 属性 进行了上述排序。您必须将其更改为评论对象 属性,您希望以此为基础进行排序。
订购评论实体的最佳做法是什么?
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;
}
在 return posts
之前,您可以订购附在每个 post 上的评论:
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;
我根据名为 DateCreated
的 属性 进行了上述排序。您必须将其更改为评论对象 属性,您希望以此为基础进行排序。