Linq to Entities - 使用 Include() 进行预加载

发布于 2024-09-08 07:53:57 字数 1089 浏览 4 评论 0原文

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

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

所以基本上,我想做的是当我加载类别时,我还想加载所有问题和所有答案。

现在,我已经能够使用以下代码来完成此操作:

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 表达式;像这样:

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 表达式吗?

I've got this really basic table structure:

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.

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡写薰衣草的香 2024-09-15 07:53:57

好的;在 此处

基本上,我需要这样做:

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 的引用。实际上,这个 lambda 表达式仅用于生成属性路径“tblQuestion.tblAnswers”,它将急切加载所有问题的答案。

因此,尽管看起来我只提取了第一个问题的答案,但实际上我提取了所有问题的所有答案。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文