简体   繁体   English

如何在LINQ表达式中包含多个层次?

[英]How can I include down more than one level in a LINQ expression?

I have the following classes: 我有以下课程:

public partial class Exam
{
    public Exam()
    {
        this.Objectives = new List<Objective>();
    }
    public int ExamId { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Objective> Objectives { get; set; }
}
public partial class Objective

    public Objective()
    {
        this.ObjectiveDetails = new List<ObjectiveDetail>();
    }

    public int ObjectiveId { get; set; }
    public int ExamId { get; set; }
    public string Text { get; set; }
    public virtual Exam Exam { get; set; }
    public virtual ICollection<ObjectiveDetail> ObjectiveDetails { get; set; }
}

public partial class ObjectiveDetail
{
    public ObjectiveDetail()
    {
        this.SubTopics = new List<SubTopic>();
    }
    public int ObjectiveDetailId { get; set; }
    public int ObjectiveId { get; set; }
    public int Number { get; set; }
    public string Text { get; set; }
    public virtual Objective Objective { get; set; }

}

I created the following to get the Exam and Objective data: 我创建了以下内容以获取考试和目标数据:

        var exam = _examsRepository.GetAll()
            .Where(e => e.Name == name)
            .Include(e => e.Objectives)
            .FirstOrDefault();

But how can I also include the ObjectiveDetails ? 但是我如何也可以包含ObjectiveDetails呢?

Like this: 像这样:

var exam = _examsRepository.GetAll()
    .Where(e => e.Name == name)
    .Include(e => e.Objectives)
    .Include(e => e.Objectives.Select(o => o. ObjectiveDetails))
    .FirstOrDefault();

MSDN article on loading related entities. 有关加载相关实体的MSDN文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM