问题描述
我搜索了一下,就知道我可以使用投影部分加载的实体,问题就来了,有没有办法,以部分预先加载一个孩子?说我有以下
I searched a bit and understands that I can use projection to partially load an entity , the question becomes is there a way to partially eager loading a child?Say I have the following
实体A有
Id
Name
EntityB
和实体B具有
Id
StuffToBeLoaded1
StuffToBeLoaded2
OtherStuffNotToBeLoaded
我如何装入A和B,而且B只有stuffToBeLoaded1和stuffToBeLoaded2?我想我不能叫.Inlucde(EntityB),否则将被完全加载,是吧?
How can I load A with B , and B only has stuffToBeLoaded1 and stuffToBeLoaded2?I guess I cannot call .Inlucde("EntityB") otherwise it is fully loaded, is it?
推荐答案
您必须使用自定义查询与预测。如果 EntityB
属性重新presents集合,你可以使用这样的:
You must use custom query with a projection. If EntityB
property represents collection you can use something like:
var query = from a in context.EntitiesA
select new
{
a.Id,
a.Name,
Bs = a.EntityB.Select(b => new {
b.StuffToBeLoaded1,
b.StuffToBeLoaded2
})
};
如果 EntityB
不是一个集合的导航属性,你可以简单地使用:
If EntityB
is not a collection navigation property you can simply use:
var query = from a in context.EntitiesA
select new
{
a.Id,
a.Name,
a.EntityB.StuffToBeLoaded1,
a.EntityB.StuffToBeLoaded2
};
这篇关于部分预先加载子实体(加载特定的个行业)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!