EF核心:嵌套预加载集合的订购依据

时间:2018-12-27 21:04:37

标签: c# entity-framework-core

我有一个用例,该用例具有深层嵌套的类层次结构,例如:

public class Parent
{
    public int Id { get; set; }

    public List<ChildOne> Children { get; set; }
}


public class ChildOne
{
    public int Id { get; set; }
    public int ParentId { get; set; }

    public List<ChildTwo> ChildrenTwo { get; set; }
}


public class ChildTwo
{
    public int Id { get; set; }
    public int Priority { get; set; }

    public int ChildOneId { get; set; }

    public List<ChildThree> ChildrenThree { get; set; }
}


public class ChildThree
{
    public int Id { get; set; }

    public int ChildTwoId { get; set; }
}

如果要加载所有父对象及其相关的子级,请执行以下操作:

var objects = context.Parent
    .Include(parent => parent.Children)
        .ThenInclude(childOne => childOne.ChildrenTwo)
            .ThenInclude(childTwo => childTwo.ChildrenThree)
    .ToList();

但是,如果我想让ChildrenTwo急切加载的导航属性中的ChildOne实体按其Priority进行排序,该怎么办?我已经进行了一些研究,并且从下面的链接(以及其他一些链接)中,显然还不能直接在EF Core中实现:

那么,如何以一种良好/简洁的方式快速实现上述ChildrenTwo的排序(通过Priority)?这可能意味着大多数工作应该在数据库服务器上进行,而不是在.NET客户端上进行。最好的方法是什么?

1 个答案:

答案 0 :(得分:0)

虽然回答还为时已晚,但它可能对将来的读者有所帮助

我将解释代码:

var authorArticles = await _context.AuthorArticles
                .Include(a => a.Author)
                .ThenInclude(p => p.Person)
                .ThenInclude(pq => pq.Qualifications)
                .ThenInclude(q => q.QualificationSubject)
                .Include(a => a.Author)
                .ThenInclude(p => p.Person)
                .ThenInclude(pp => pp.Professions)
                .Include(a => a.Author)
                .ThenInclude(p => p.Person)
                .ThenInclude(pp => pp.Professions)
                .ThenInclude(prof => prof.Profession)
                .Where(aa => aa.ArticleId == articleId)
                .Select(s => new AuthorArticle
                {
                    Author = new Author
                    {
                        Affiliation = s.Author.Affiliation,
                        AvailableAsReviewer = s.Author.AvailableAsReviewer,
                        Person = new Person
                        {
                            Email = s.Author.Person.Email,
                            FirstName = s.Author.Person.FirstName,
                            LastName = s.Author.Person.LastName,
                            MiddleName = s.Author.Person.MiddleName,
                            Title = s.Author.Person.Title,
                            FullName = s.Author.Person.FullName,
                            UserId = s.Author.Person.UserId,
                            Professions = new Collection<PersonProfession>
                            {
                                new PersonProfession
                                {
                                    // using sorting here!!
                                    Organization = s.Author.Person.Professions
                                        .OrderByDescending(pid => pid.ProfessionId)
                                        .FirstOrDefault().Organization,
                                    Profession = s.Author.Person.Professions
                                        .OrderByDescending(pid => pid.ProfessionId)
                                        .FirstOrDefault().Profession
                                }
                            },
                            Qualifications = new Collection<PersonQualification>
                            {
                                new PersonQualification
                                {
                                    QualificationSubject = s.Author.Person.Qualifications
                                        .OrderByDescending(q => q.QualificationLevelId)
                                        .FirstOrDefault().QualificationSubject,
                                    QualificationLevelId = s.Author.Person.Qualifications
                                        .OrderByDescending(q => q.QualificationLevelId)
                                        .FirstOrDefault().QualificationLevelId
                                }
                            }
                        }
                    },
                    IsCorresponding = s.IsCorresponding,
                    AuthorPosition = s.AuthorPosition
                }).ToListAsync();

            return authorArticles;

如果您只是渴望加载实体,那么在投影时;这意味着当您从查询中选择项目时,可以重新创建已经以略有不同的方式提供的对象。就我而言,我只希望其中的一种职业,而且对个人资格的追求也是如此。

Another SO great answer!的选择中寻求帮助