开发者

c# Linq query question with EF4 table per type Inheritance

I have been searching for way to perform a linq query to get an o开发者_StackOverflow中文版bject graph based on a specific child inheritance type. I may have prased this a little wrong, but I will try to explain.

Lets say I have a EF4 model that has a City entity, a subdivision entity, a house entity and a feature entity. The city has a one to many relationship with subdivision. The subdivision has a one to many relationship to a house. And a house has a one to many relationship with a feature. Two entities subtype the feature entity. Lets call them pool and driveway.

What would the c# linq query be to query the object graphs for all cities that have houses with a feature.OfType "Pool" with city at the root of the graph?

similar to: var cities = from city in context.Cities where ?????? select city


You need to use the IQueryable.OfType method to restrict which type you are actually interested in working with. The EF4 IQueryable provider will take care of the rest, and generate the proper SQL queries for you.

Queryable.OfType<T>

The following query should work (assuming you have all the proper navigation relationships defined in your model):

var cities = from city in context.Cities
             from house in city.Houses
             from pool in house.Features.OfType<Pool>
             where pool.MaxDepth == 6
             select city;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜