Reputation: 43
How do I include a 2nd level table column in my linq query? I don't want .Net to perform lazy loading because there are other tables linked to these tables.
The table are
Quiz:
- Id
- Name
Questions:
- Id
- Name
- quizId
Options:
- id
- Name
- QuestionId
Quiz and Questions have a one-to-many relationship. Questions to Option also have a one-to-many relationship.
var quiz=db.Quiz.include(a=>a.Questions)......ToList();
How can I include Options columns in my linq query?
Upvotes: 4
Views: 2868
Reputation: 23690
I'm pretty sure that you can just do multiple Include()
s:
var quiz = db.Quiz
.Include(x => x.Questions)
.Include("Questions.Options")
.ToList();
It's not completely strongly typed though like @pwee167's answer, however it is descriptive for each collection that you want to have included in your query.
Upvotes: 0
Reputation: 11
Add to Questions property Options as ICollection and mapping as one to many. Then You may use
var quiz=db.Quiz.include(a=>a.Questions).include(a=>a.Questions.Options).......ToList();
Or
Upvotes: 0
Reputation: 4197
If you want to eargerly load grandchildren of an entity, the code below will achieve this, provided you have mapped the relationships correctly.
var quiz = db.Quiz
.Include(a => a.Questions.Select(q => q.Options))
.ToList();
Upvotes: 2