Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq-to-entities - Include() method not loading

If I use a join, the Include() method is no longer working, eg:

from e in dc.Entities.Include("Properties") join i in dc.Items on e.ID equals i.Member.ID where (i.Collection.ID == collectionID)  select e 

e.Properties is not loaded

Without the join, the Include() works

Lee

like image 609
Lee Atkinson Avatar asked Apr 27 '09 16:04

Lee Atkinson


2 Answers

UPDATE: Actually I recently added another Tip that covers this, and provides an alternate probably better solution. The idea is to delay the use of Include() until the end of the query, see this for more information: Tip 22 - How to make include really include


There is known limitation in the Entity Framework when using Include(). Certain operations are just not supported with Include.

Looks like you may have run into one on those limitations, to work around this you should try something like this:

var results =     from e in dc.Entities //Notice no include    join i in dc.Items on e.ID equals i.Member.ID    where (i.Collection.ID == collectionID)     select new {Entity = e, Properties = e.Properties}; 

This will bring back the Properties, and if the relationship between entity and Properties is a one to many (but not a many to many) you will find that each resulting anonymous type has the same values in:

anonType.Entity.Properties anonType.Properties 

This is a side-effect of a feature in the Entity Framework called relationship fixup.

See this Tip 1 in my EF Tips series for more information.

like image 125
Alex James Avatar answered Oct 17 '22 01:10

Alex James


Try this:

var query = (ObjectQuery<Entities>)(from e in dc.Entities             join i in dc.Items on e.ID equals i.Member.ID             where (i.Collection.ID == collectionID)              select e)  return query.Include("Properties")  
like image 45
Bryan2010 Avatar answered Oct 17 '22 01:10

Bryan2010