尝试将我们的 EF4 解决方案切换到 EF CTP5,但遇到了问题。
这是模型的相关部分:
相关关系:
- 一个县拥有许多城市
- 单个城市有单个州
现在,我想执行以下查询:
- 获取系统中的所有县,包括所有城市,以及这些城市的所有州。
在 EF4 中,我会这样做:
var query = ctx.Counties.Include("Cities.State");
在 EF CTP5 中,我们有一个强类型的 Include,它采用 Expression>
。
我可以毫无问题地获取县的所有城市:
var query = ctx.Counties.Include(x => x.Cities);
但是我如何才能获取这些城市的州?
我使用的是纯 POCO,因此 County.Cities
是一个 ICollection
,因此我无法执行此操作:
var query = ctx.Counties.Include(x => x.Cities.State)
正如 ICollection
所做的那样没有名为 State
的属性。
这几乎就像我需要使用嵌套的 IQueryable 一样。
有什么想法吗?在这种情况下我需要回退到魔术字符串 Include 吗?
Attempting to cutover our EF4 solution to EF CTP5, and ran into a problem.
Here's the relevant portion of the model:
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.
In EF4, i would do this:
var query = ctx.Counties.Include("Cities.State");
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?
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)
As ICollection<City>
does not have a property called State
.
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?
发布评论
评论(1)
为此,您可以使用Select方法:
在这里您可以找到另一个示例。
For that you can use you the Select method:
Here you can find another example.