LINQ到实体 - 使用包括立即加载() [英] Linq to Entities - eager loading using Include()

查看:140
本文介绍了LINQ到实体 - 使用包括立即加载()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这有非常基本的表结构:

I've got this really basic table structure:

dbo.tblCategory
dbo.tblQuestion(多对一关系tblCategory)
dbo.tblAnswer(多对一的关系,tblQuestion)

dbo.tblCategory
dbo.tblQuestion (many to one relationship to tblCategory)
dbo.tblAnswer (many to one relationship to tblQuestion)

因此​​,基本上,当我打开一个类别,我也想加载所有的问题,所有的答案是我想要做的是。

So basically, what I'm trying to do is when I load a category, I want to also load all Questions, and all Answers.

现在,我已经能够使用下面的code要做到这一点:

Now, I've been able to do this using the following code:

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

            return entities.DetachObjectGraph(dto);
        }
    }
}

不过,我不能完全迷恋这一点;如果关系的名字在我的模型变化;我不打算建设项目时得到一个错误。理想情况下,我想使用lambda EX pression;是这样的:

However, I'm not completely enamored with this; if the relationship names change in my model; I'm not going to get a error when building the project. Ideally, I'd like to use a lambda expression; something like this:

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

        return entities.DetachObjectGraph(dto);
    }
}

现在,上述代码段;我卡在如何深入到答案表。在我可以用什么的lambda EX pression这一个?

Now, with the above snippet; I'm stuck on how to drill down to the Answers table. Any idea on what I could use for a lambda expression for this one?

推荐答案

确定;我能得到这个工作,从的这里

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

基本上,我需要做的:

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

从上面的链接,我只能提领的问题,收集的各个项目tblAnswers。在这里,我选择了取消引用tblAnswers对集合的第一项。在现实中,这拉姆达EX pression仅仅是用于生产的属性路径tblQuestion.tblAnswers​​,这将贪婪加载的所有问题的答案。

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.

这篇关于LINQ到实体 - 使用包括立即加载()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆