LINQ: querying multiple entities

  • Thread starter Thread starter MCohen75
  • Start date Start date
M

MCohen75

I have a couple entities setup with associations. A ReleasePackage has many
Conent entities. For the ReleasePackages in the list, I want to get all
Conent entities. This is like a simple join, here's the code:

IList<Content> contentList =
(from p in this.ReleasePackage
from c in p.Content
select c).ToList<Content>();

For some reason this doesn't work, I get zero results in the list.

I did call Load on the ReleasePackage (which is an association from another
entity) prior to the statement above.

If I manually iterate over the ReleasePackage entities and access the
Content association on each, I get the correct records.

What gives?

--Mike
 
It looks like I have to explicitly load each association. If i iterate over
the list of release packages and load the associations to Content, the
statement below works.

With EF can I specify that I want to automatically load some associations?

--Mike
 
MCohen75 said:
I have a couple entities setup with associations. A ReleasePackage has many
Conent entities. For the ReleasePackages in the list, I want to get all
Conent entities. This is like a simple join, here's the code:

IList<Content> contentList =
(from p in this.ReleasePackage
from c in p.Content
select c).ToList<Content>();

For some reason this doesn't work, I get zero results in the list.

I did call Load on the ReleasePackage (which is an association from another
entity) prior to the statement above.

If I manually iterate over the ReleasePackage entities and access the
Content association on each, I get the correct records.

If this is linq to sql, you should look into LoadOptions to tell linq
to sql to load associated entities as well. It's only efficient 1 level
deep.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
Back
Top