I think that there\'s a similar post on here about this but not exactly the same...
I have two entities in my EF model - let\'s call them Person and Developer, with
How about this:
var results = from developer in ctx.Employee.OfType<Developer>()
select new {developer, Qualifications = developer.Qualifications};
To things are interesting here:
if you subsequently do this:
var developers = from anon in developers.AsEnumerable()
select anon.Developer;
You will get just developers, and they will have their Qualifications loaded too.
See Tip 1 for more info on why this works
Hope this helps
Alex
Based on Tri Q's answer, but for the ones that prefer the '.Load()' approach, you can use:
context.Persons.OfType<Developer>().Include(t => t.Qualifications).Load();
I use it inside my DbContext. I created LoadDb() method, that loads all the data I need. So I can use it from many projects.
I'm not familiar with EF but this looks to me like a standard inheritance problem. If you want to access the unique properties of a child class from a collection of 'parent' objects (where the actual objects may or may not be instances of the child class) then you need to check the object type and cast it to the child where appropriate, then you can access the unique properties.
What would you expect the given statement to return if some or all of the objects were person not developer?
I've come across this problem and experimented a little. Here is what i found worked using LINQ 2 Entity.
Say we have in your example Person <-- Developer ---- Qualifications.
If you would like to select a Developer with Qualifications included, you would do this.
var dev = (from d in context.Persons.OfType<Developer>
.Include("Qualifications")
where d.ID == id
select d).FirstOfDefault();
Now lets say we have another association between Person and Address, we can also include Address into the select also using LINQ 2 Entity.
var dev = (from d in context.Persons
.Include("Address")
.OfType<Developer>()
.Include("Qualifications")
where d.ID == id
select d).FirstOfDefault();
Notice how I have included the things I needed before I converted the Type and also included again after the conversion. Both includes should now work together without any problems. I have tested these methods and they both work. I hope they work for you given you have your inheritance setup correctly.
GL.