实体框架 - 包含在子查询中? - 第2部分 [英] Entity Framework - Include in sub query? - Part 2
问题描述
我不知道这是否是正确的事情,我相信有人会告诉我,如果不是。
我问了一个问题(<一个href =https://stackoverflow.com/questions/1662760/entity-framework-include-in-sub-query>实体框架 - 包含在子查询中)今天晚上,这是非常回答的好了,解决了我的问题。但是,我认为可能会有一个更好的方法,所以我要重新提出这个问题,但是略有不同。
我有3个表: / p>
餐厅1 ..... M MenuCategory 1 ..... M MenuItem
我有一个看起来像这样的L2E查询:
Restaurant = context.Restaurant
.Include(r => r.MenuCategory)
.FirstOrDefault(r => r.RestaurantId == resaurantId);
哪些工作在某种程度上,但它只能预先加载菜单类别。
我真正想要做的是这样的: p>
Restaurant = context.Restaurant
.Include(r => r.MenuCategory)
.Include(r = r.MenuCategory.MenuItems)
.FirstOrDefault(r => r.RestaurantId == resaurantId);
但显然这不可用,因为r.MenuCategory是一个可枚举的
...的工作是使用标准符号:
context.Restaurant.Include ( MenuCategory.MenuItems);
...但这不是强类型的。这个问题是关于发现一个强类型答案
这是目前的扩展方法:
public static ObjectQuery< T>包含< T>(此ObjectQuery< T>查询,表达式< Func< T,对象>>路径)
{
//检索成员路径:
列表< PropertyInfo> members = new List< PropertyInfo>();
EntityFrameworkHelper.CollectRelationalMembers(path,members);
//构建字符串路径:
StringBuilder sb = new StringBuilder();
string separator =;
foreach(成员中的MemberInfo成员)
{
sb.Append(separator);
sb.Append(member.Name);
separator =。;
}
//应用Include:
return query.Include(sb.ToString());
}
这可以如何适应以允许强类型的形式:
context.Restaurant.Include(MenuCategory.MenuItems);
我有一个提示,正是这样:提示28 - How实施一个热切的负载策略
使用一个漂亮的技巧我认为。
Alex
I'm not sure if this is the right thing to do, I'm sure someone will tell me if it's not.
I asked a question (Entity Framework - Include in sub query?) earlier this evening, which was answered very well and has solved my problem. But, I think there could be a better way, so I'm going to re-ask the question, but slightly differently.
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.
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
...the work around is to use the standard notation:
context.Restaurant.Include("MenuCategory.MenuItems");
...but this is not strongly typed. This question is about finding a strongly typed answer
This is the current extension method:
public static ObjectQuery<T> Include<T>(this ObjectQuery<T> query, Expression<Func<T, object>> path)
{
// Retrieve member path:
List<PropertyInfo> members = new List<PropertyInfo>();
EntityFrameworkHelper.CollectRelationalMembers(path, members);
// Build string path:
StringBuilder sb = new StringBuilder();
string separator = "";
foreach (MemberInfo member in members)
{
sb.Append(separator);
sb.Append(member.Name);
separator = ".";
}
// Apply Include:
return query.Include(sb.ToString());
}
How could this be adapted to allow a strongly typed form of:
context.Restaurant.Include("MenuCategory.MenuItems");
I have a Tip that allows exactly this: Tip 28 - How to implement an eager load strategy
Uses a nifty trick I think.
Alex
这篇关于实体框架 - 包含在子查询中? - 第2部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!