c# Linq query question with EF4 table per type Inheritance
I have been searching for way to perform a linq query to get an object 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;
链接地址: http://www.djcxy.com/p/90194.html
上一篇: 如何不坚持财产EF4代码第一?