Linq to Entities - eager loading using Include()

别说谁变了你拦得住时间么 提交于 2019-11-30 19:21:13
Jim B

OK; I was able to get this to work, with some help from here.

Basically, I need to do this:

public tblCategory Retrieve(int id)
{
    using (var entities = Context)
    {
        var dto =
            (from t in entities.tblCategory.Include(t => t.tblQuestion)
                                           .Include(t => t.tblQuestion.First().tblAnswer)
             where t.Id == id
             select t).FirstOrDefault();

        return entities.DetachObjectGraph(dto);
    }
}

From the link above, I can only dereference tblAnswers on individual items of the questions collection. Here I chose to dereference tblAnswers on the first item of the collection. In reality, this lambda expression is merely used to produce the property path “tblQuestion.tblAnswers”, which will eager load the answers of all questions.

So even though it looks like I'm only pulling the answers for the first question, I'm actually pulling all the answers for all the questions.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!