1

我有以下课程:

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 ?

4

1 回答 1

0

像这样:

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

关于加载相关实体的 MSDN 文章。

于 2013-11-02T15:15:20.870 回答