实体框架 - 包含在子查询中? [英] Entity Framework - Include in sub query?

查看:108
本文介绍了实体框架 - 包含在子查询中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这是否已经回答了,我看了几个问题,但我不认为他们是我以前的事。



假设我有3个表:

 餐厅1 ..... M MenuCategory 1 ..... M MenuItem 

我有一个看起来像这样的L2E查询:



= pre> 餐厅= );

哪些在某种程度上有效,但只能预先加载菜单类别。



作为一个工作,我可以遍历每个类别并调用它们.Load(),但这将涉及到更多的理论上应该需要的。



我真正想要做的是这样的:

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

但显然这不可用,因为r.MenuCategory是一个可枚举的



ANSWER 1:



context.Restaurant.Include(MenuCategory.MenuItems);




  • 这样做有效,但它不是强类型的。我想知道是否有人能够提出强力类型的第二个答案(我可能会将其转移到另一个问题,因为这已被回答并且回答良好。



我已经把这个转移到另外一个问题,因为我觉得从一个完美的答案中解脱出来是不公平的,而且应该是完美的:



实体框架 - 包含在子查询中? - 第2部分

解决方案

此链接似乎解决了您的问题?

  var result = context。 Restaurant.Include(MenuCategory.MenuItems); 


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:

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

I have a L2E query 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 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.

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

ANSWER 1:

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

  • 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.

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

解决方案

This link here seems to resolve your problem ?

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

这篇关于实体框架 - 包含在子查询中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆