0

I need to eager load the ExceptionAppointment in the following scenario.

I have 3 tables: Appointment, ExceptionOccurrence, ExceptionAppointment with the following relationships

  • Appointment has a 1:m relationship with ExceptionOccurrence
  • ExceptionOccurrence has a 1:1 relationship with ExceptionAppointment

I know I can eager load the ExecptionOccurrence using

context.Appointments.Include(a => a.ExceptionOcurrences).ToList();

but how do I change the expression to include the ExceptionAppointment as well?

Cheers Abs

user1460473
  • 115
  • 9
  • Have you tried `context.Appointments.Include(a => a.ExceptionOcurrences.ExceptionAppointment).ToList();`? – LukLed Jun 17 '12 at 21:46
  • There is no option on the ExceptionOcurrences for an ExceptionAppointment. Should there be? – user1460473 Jun 17 '12 at 22:01
  • 1
    Sorry: `context.Appointments.Include("ExceptionOcurrences.ExceptionAppointment").ToList()`. Does ExceptionOccurence have ExceptionAppointment navigation property? – LukLed Jun 17 '12 at 22:12
  • Doesnt seem to work :( Yes the ExceptionOccurrence does have an ExceptionAppointment navigation property. – user1460473 Jun 17 '12 at 22:26
  • @LukLed: BTW I am using this approach (http://stackoverflow.com/questions/11071942/ef-4-3-code-first-custom-icollection-fails-to-catch-new-items). Maybe at the point of the InsertItem (for the ExceptionOccurrence) the ExceptionAppointment hasnt been loaded? – user1460473 Jun 17 '12 at 22:29
  • What error does it throw (the second solution, with include as string)? – LukLed Jun 17 '12 at 22:32
  • @LukLed: **My mistake**. You are correct :) BTW, can your Include statement be expressed in a lambda? – user1460473 Jun 17 '12 at 22:34
  • 1
    Maybe `context.Appointments.Include(a => a.ExceptionOcurrences.Select(eo => eo.ExceptionAppointment)).ToList();`? I don't have access to EF 4 now unfortunately. – LukLed Jun 17 '12 at 22:40
  • Works perfectly!! Thanks for taking the time to look into this (much appreciated) – user1460473 Jun 17 '12 at 22:52

1 Answers1

2

So the answer was (for people who don't read comments):

Without Lambda:

context.Appointments.Include("ExceptionOcurrences.ExceptionAppointment").ToList‌​()

With Lambda:

context.Appointments.Include(a => a.ExceptionOcurrences.Select(eo => eo.ExceptionAppointment)).ToList();
LukLed
  • 31,452
  • 17
  • 82
  • 107