Ok, I have tri-leveled entities with the following hierarchy: Course -> Module -> Chapter
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?
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);
Your solution fails because Include
doesn't take a boolean operator
Include(i => i.Modules.Select(s => s.Chapters) && i.Lab)
^^^ ^ ^
list bool operator other list
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.
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);
But the samples in LinqPad explains this better.