Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework - Include in sub query?
    primarykey
    data
    text
    <p>I'm not sure if this has been answered yet, I looked at a couple of questions but I don't think they were quite what I was after.</p> <p>Let's say I have 3 tables:</p> <pre><code>Restaurant 1.....M MenuCategory 1.....M MenuItem </code></pre> <p>I have a L2E query that looks something like this:</p> <pre><code>Restaurant = context.Restaurant .Include(r =&gt; r.MenuCategory) .FirstOrDefault(r =&gt; r.RestaurantId == resaurantId); </code></pre> <p>Which works to some extent, but it only pre-loads the menu categories.</p> <p>As a work around I am able to iterate around each category and call .Load() on them, but this will involve hitting a lot more that in theory I should need to.</p> <p>What I really want to be able to do is something like:</p> <pre><code>Restaurant = context.Restaurant .Include(r =&gt; r.MenuCategory) .Include(r =&gt; r.MenuCategory.MenuItems) .FirstOrDefault(r =&gt; r.RestaurantId == resaurantId); </code></pre> <p>But clearly this isn't available as r.MenuCategory is an enumerable</p> <p>ANSWER 1:</p> <p>context.Restaurant.Include("MenuCategory.MenuItems");</p> <ul> <li>This works, but it is not strongly typed. I wonder if anyone is able to come up with a second answer that is strongly typed (I may move this to another question as this has been answered, and answered well.</li> </ul> <p>I have moved this to another question as I felt it was unfair to take away from an answer that is perfect and works exactly as it should:</p> <p><a href="https://stackoverflow.com/questions/1663783/entity-framework-include-in-sub-query-part-2">Entity Framework - Include in sub query? - Part 2</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload