0

Trying to get child of child, both with many relationship.

So a CarePeriod has many DailyExerciseSets which has many ExerciseTrials. so carePeriod has collection of DailyExerciseSets which has a collection of ExerciseTrials SQL I would write is

SELECT <bla>
FROM careperiods 
 left outer JOIN DailyExerciseSets ON (...) 
 left outer JOIN exerciseTrials ON (...)
WHERE
 careperiods.bla = bla
 AND DailyExerciseSets.bla = bla
 AND exerciseTrials.bla = bla

Just need to replicate this logic in EF/LINQ. I have worked around this issue a couple times but really want to see how this is meant to be accomplished.

intuitively i would have gone for

var x = (from cp in db.carePeriod
.include(cp => cp.DailyExerciseSets) //this bit is fine
.include(cp => cp.DailyExerciseSets.exerciseTrials) //fail...
where (conditions...)
select cp);

but this fails..

A point in the right direction would be great.

michaelBurns
  • 77
  • 1
  • 10
  • use the mentioned link http://stackoverflow.com/questions/3356541/entity-framework-linq-query-include-multiple-children-entities – Dhaval Patel May 19 '14 at 06:37

1 Answers1

0

To include collections you need to use a "sub select" and not the dot-notation:

var x = (from cp in db.carePeriod
             .Include(cp => cp.DailyExerciseSets)
             .Include(cp => cp.DailyExerciseSets.Select(es => es.exerciseTrials))
         where (conditions...)
         select cp);

Also you do not need Include(cp => cp.DailyExerciseSets) here, as this is already included by the "child include":

var x = (from cp in db.carePeriod
             .Include(cp => cp.DailyExerciseSets.Select(es => es.exerciseTrials))
         where (conditions...)
         select cp);
Christoph Fink
  • 22,727
  • 9
  • 68
  • 113