How to Include Property
I have the following example:
using (MyContext context = new MyContext())
{
var query = from entityA in context.EntityA.Include("TestProperty")
join entityB in context.EntityB on entityA.Id equals entityB.EntityAId
join entityC in context.EntityC on entityB.Id equals entityC.EntityBId
开发者_开发问答 where entityC.Id == id
select entityA;
List<EntityA> toReturn = query.ToList();
return toReturn;
}
My joins are all working as expected, however, my 'TestProperty' navigation property is not loaded properly. I look, during runtime, and it is null.
When I do the following:
context.LoadProperty(toReturn, "TestProperty");
It loads the 'TestProperty' property correctly. Is there something I am doing incorrectly in my LINQ-to-SQL statement?
UPDATE: I changed the first line to be:
from entityA in context.EntityA.Include("TestProperty").ToList()
And left the rest of the lines below that the same and it loaded my property correctly. Is this the proper solution to the problem?
"Is this the proper solution to the problem?" it is the worst possible solution, because your are now doing the whole query client-side.
Include must be written after join & where clause
Check this:
var query = ((from entityA in context.EntityA
join entityB in context.EntityB on entityA.Id equals entityB.EntityAId
join entityC in context.EntityC on entityB.Id equals entityC.EntityBId
where entityC.Id == id
select entityA) as objectQuery<EntityA>).Include("TestProperty")
精彩评论