简体   繁体   English

在实体框架中订购包含实体的最佳方式?

[英]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.

相关问题 在实体框架中过滤子实体的最佳方法 - Best Way To Filtering Child Entities In Entity Framework 在实体框架中将外键列表链接到实体列表的最佳方法 - Best way to link a list of foreign keys to a list of entities in entity framework 实体框架:包含子实体的 AsNoTracking - Entity Framework: AsNoTracking for included child entities 在 Entity Framework 6 中过滤通用包含的实体 - Filter generic included entities in Entity Framework 6 相关实体仅间歇地包含在Entity Framework查询中 - related entities included only intermittently in Entity Framework query 使用接口的最佳方法,该接口在wcf数据服务中实现实体框架实体 - best way to consume interface that implement entity framework entities in wcf data services 使用实体框架和LINQ到实体时分配外键的最佳方法是什么? - What is the best way in assigning foreign key when using entity framework & LINQ to Entities? 有没有办法在实体框架中映射到不可变实体? - Is there a way to map to immutable entities in entity framework? 实体框架实体的最佳做法将覆盖Equals和GetHashCode - Best practices for Entity Framework entities override Equals and GetHashCode 在实体上执行CRUD操作的最佳实践(实体框架) - Best practices to perform CRUD operations on Entities (Entity Framework)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM