EF LINQ包括多个和嵌套实体 [英] EF LINQ include multiple and nested entities
问题描述
好吧,我有以下层次结构三层次的实体:课程 - >模块 - >第一章
Ok, I have tri-leveled entities with the following hierarchy: Course -> Module -> Chapter
下面是原来的EF LINQ语句:
Here was the original EF LINQ statement:
Course course = db.Courses
.Include(i => i.Modules.Select(s => s.Chapters))
.Single(x => x.Id == id);
现在,我想包括所谓的实验室另一个实体这与课程相关的。
Now, I want to include another entity called Lab which is associated with a course.
我如何包括实验室实体?
How do I include the Lab entity?
我试过以下,但它没有工作:
I tried the following but it didn't work:
Course course = db.Courses
.Include(i => i.Modules.Select(s => s.Chapters) && i.Lab)
.Single(x => x.Id == id);
这包括第二实体的任何想法?
Any ideas on including the 2nd Entity?
任何一块意见或信息将是非常美联社preciated。谢谢!
Any piece of advise or information would be highly appreciated. Thanks!
推荐答案
您是否尝试过只需添加另一个包含
:
Have you tried just adding another Include
:
Course course = db.Courses
.Include(i => i.Modules.Select(s => s.Chapters))
.Include(i => i.Lab)
.Single(x => x.Id == id);
由于包括您的解决方案失败
不采取布尔运算符
Include(i => i.Modules.Select(s => s.Chapters) && i.Lab)
^^^ ^ ^
list bool operator other list
更新
要了解更多信息,请下载 LinqPad 并通过样本的样子。
我认为这是熟悉的LINQ和Lambda的最快方式。
Update To learn more, download LinqPad and look through the samples. I think it is the quickest way to get familiar with Linq and Lambda.
作为一个开始 - 选择之间的差异
和包含
是与一个选择决定的的 的您想返回(又名投影)的东西。在include是一个 预先加载的功能,告诉你希望它包括来自其他表中的数据实体框架。
As a start - the difference between Select
and Include
is that that with a Select you decide what you want to return (aka projection). The Include is a Eager Loading function, that tells Entity Framework that you want it to include data from other tables.
在包括语法,也可以在字符串。像这样的:
The Include syntax can also be in string. Like this:
db.Courses
.Include("Module.Chapter")
.Include("Lab")
.Single(x => x.Id == id);
但在 LinqPad 样解释这更好的。
这篇关于EF LINQ包括多个和嵌套实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!