如何在LINQ表达式中包含多个级别?

时间:2013-11-02 15:10:51

标签: c# linq

我有以下课程:

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; }

}

我创建了以下内容以获取考试和目标数据:

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

但我怎样才能包含ObjectiveDetails?

1 个答案:

答案 0 :(得分:0)

像这样:

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.