EF CTP5 - Strongly-Typed Eager Loading - How to Include Nested Navigational Properties?

…衆ロ難τιáo~ 提交于 2019-12-17 22:14:21

问题


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:


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.



来源:https://stackoverflow.com/questions/4811557/ef-ctp5-strongly-typed-eager-loading-how-to-include-nested-navigational-prop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!