在.net core 2.2中 我有一个像这样的模型:
public class A
{
public ICollection<B> B { get; set; }
}
public class B
{
public ICollection<C> C { get; set; }
public ICollection<D> D { get; set; }
}
public class C
{
}
public class D
{
}
查询是这样的:
var query = from record in Set<A>()
where record.Id == Id
select new AProjection
{
BProjection =( from bRecord in Set<B>()
where bRecord.Id = record.BId
select new BProjection
{
CProjection = bRecord.C.Select( b => new CProjection {/* data here */ }),
DProjection = bRecord.C.Select( b => new DProjection {/* data here */ })
})
}
我想将数据返回到投影中,但是对于类型B的每个对象,都在进行查询以获取类型C的对象,并通过另一个查询来获取类型D的对象。如何避免这种行为?
这是此问题解决的典型N + 1查询问题: