如果您将排序顺序设置为有意义的,那么这应该是有效的。(
DataLoadOptions.AssociateWith() Reference
)
IList<Parent> results;
using (DataBaseContext db = new MyDb())
{
var dlo = new DataLoadOptions();
dlo.LoadWith<Parent>(x => x.Child1); // We only want the most recent 10.
dlo.AssociateWith<Parent>(x => x.Child1.OrderByDescending(c => c.Date).Take(10));
dlo.LoadWith<Parent>(x => x.Child2); // All of these...
dlo.LoadWith<Parent>(x => x.Child3); // Only the most recent 1.
dlo.AssociateWith<Parent>(x => x.Child3.OrderByDescending(c => c.Date).Take(1));
db.LoadOptions = dlo;
results = (from p in Parent
orderby p.Id descending
select p).Take(5).ToList();
}
纯克罗姆编辑
请注意(阅读本文的任何人)如果使用AssociateWith方法,则必须使用LoadWith对其进行预处理。注意我们是如何装载(child1)和下一行我们关联的(…一些时髦的屁股lambda)??这很好->如果您忘记在AssociateWith之前放入loadWith,则不会生成任何SQL,也不会为该子级返回任何内容。