[英]Entity Framework - Include in sub query?
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. 我不确定这是否已经回答了,我看了几个问题,但我认为它们不是我所追求的。
Let's say I have 3 tables: 假设我有3个表:
Restaurant 1.....M MenuCategory 1.....M MenuItem
I have a L2E query that looks something like this: 我有一个L2E查询,看起来像这样:
Restaurant = context.Restaurant
.Include(r => r.MenuCategory)
.FirstOrDefault(r => r.RestaurantId == resaurantId);
Which works to some extent, but it only pre-loads the menu categories. 这在某种程度上起作用,但它只预加载菜单类别。
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. 作为一种解决方法,我能够遍历每个类别并在它们上调用.Load(),但这将涉及到理论上我应该需要的更多内容。
What I really want to be able to do is something like: 我真正希望能做的是:
Restaurant = context.Restaurant
.Include(r => r.MenuCategory)
.Include(r => r.MenuCategory.MenuItems)
.FirstOrDefault(r => r.RestaurantId == resaurantId);
But clearly this isn't available as r.MenuCategory is an enumerable 但显然这不可用,因为r.MenuCategory是一个可枚举的
ANSWER 1: 答案1:
context.Restaurant.Include("MenuCategory.MenuItems"); context.Restaurant.Include( “MenuCategory.MenuItems”);
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: 我把这个问题转移到了另一个问题上,因为我认为从一个完美的答案中拿出一个完全正确的答案是不公平的:
Entity Framework - Include in sub query? 实体框架 - 包含在子查询中? - Part 2
- 第2部分
You can still use the Strongly typed version to do it. 您仍然可以使用强类型版本来执行此操作。 Just use:
只需使用:
Restaurant = context.Restaurant
.Include(r => r.MenuCategory.Select(m => m.MenuItems))
.FirstOrDefault(r => r.RestaurantId == resaurantId);
这里的链接似乎解决了你的问题?
var result = context.Restaurant.Include("MenuCategory.MenuItems");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.