EF CTP5-强类型热切加载-如何包括嵌套的导航属性? [英] EF CTP5 - Strongly-Typed Eager Loading - How to Include Nested Navigational Properties?
问题描述
试图将我们的EF4解决方案转换为EF CTP5,并遇到问题.
Attempting to cutover our EF4 solution to EF CTP5, and ran into a problem.
这是模型的相关部分:
相关关系: -一个县有许多城市 -单个城市具有一个单个州
The pertinent relationship: - A single County has many Cities - A single City has a single State
现在,我要执行以下查询: -获取系统中的所有县,并包括所有城市以及这些城市的所有州.
Now, i want to perform the following query: - Get all Counties in the system, and include all the Cities, and all the State's for those Cities.
在EF4中,我会这样做:
In EF4, i would do this:
var query = ctx.Counties.Include("Cities.State");
在EF CTP5中,我们有一个强类型的包含,它包含一个Expression<Func<TModel,TProperty>>
.
In EF CTP5, we have a strongly typed Include, which takes an Expression<Func<TModel,TProperty>>
.
我可以毫无问题地获得该县的所有城市:
I can get all the Cities for the County no problem:
var query = ctx.Counties.Include(x => x.Cities);
但是我如何才能获得这些城市的州?
But how can i get the State for those Cities too?
我使用的是纯POCO,所以County.Cities
是ICollection<City>
,因此我不能这样做:
I am using pure POCO's, so County.Cities
is an ICollection<City>
, therefore i cannot do this:
var query = ctx.Counties.Include(x => x.Cities.State)
由于ICollection<City>
没有名为State
的属性.
几乎就像我需要使用嵌套的IQueryable.
It's almost like i need to use a nested IQueryable.
有什么想法吗?在这种情况下,我是否需要退回到魔术字符串包括"?
Any ideas? Do i need to fallback to the magic string Include in this scenario?
推荐答案
为此,您可以使用 Select 方法:
For that you can use you the Select method:
var query = ctx.Counties.Include(x => x.Cities.Select(c => c.State))
此处 可以找到另一个例子.
Here you can find another example.
这篇关于EF CTP5-强类型热切加载-如何包括嵌套的导航属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!