Entity Framework - include in subquery?

I'm not sure if they answered yet, I examined a couple of questions, but I do not think that they were what I was.

Let's say I have 3 tables:

Restaurant 1.....M MenuCategory 1.....M MenuItem

I have an L2E request that looks something like this:

Restaurant = context.Restaurant
   .Include(r => r.MenuCategory)
   .FirstOrDefault(r => r.RestaurantId == resaurantId);

Which works to some extent, but it only preloads the menu categories.

As a job, I can iterate through each category and call them .Load (), but this will include much more, which theoretically I will need.

What I really want to do is something like:

Restaurant = context.Restaurant
   .Include(r => r.MenuCategory)
   .Include(r => r.MenuCategory.MenuItems)
   .FirstOrDefault(r => r.RestaurantId == resaurantId);

But it is clear that this is not available as r.MenuCategory is enumerated

ANSWER 1:

context.Restaurant.Include ("MenuCategory.MenuItems");

  • , . , - , ( , , .

, , , :

Entity Framework - ? - 2

+3
2

, , ?

var result = context.Restaurant.Include("MenuCategory.MenuItems");
+2

. :

    Restaurant = context.Restaurant
    .Include(r => r.MenuCategory.Select(m => m.MenuItems))
    .FirstOrDefault(r => r.RestaurantId == resaurantId);
+7

Source: https://habr.com/ru/post/1721720/


All Articles