有条件的急切加载? [英] Conditional eager loading?
本文介绍了有条件的急切加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想加载一个实体,它是有条件的孩子(我只想在child.IsActive == true时加载孩子)。如何执行以下操作?
I want to load an entity and it's children conditionally (I only want to eager load the children when the child.IsActive == true). How do I perform the following?
var parent =
from p in db.tblParents.Include("tblChildren") <-- where tblChildren.IsActive == true
where p.PrimaryKey == 1
select p;
注意:我不想返回匿名类型。
NOTE: I do not want to return an anonymous type.
谢谢。
推荐答案
这样做的一个方法是:
var parent = from p in db.tblParents where p.PrimaryKey == 1
select new {
Parent = p,
Children = p.tblChildren.Where(c => c.IsActive == true)
}.ToList();
但是,您可能不喜欢返回匿名类型的想法,那么我建议以这种方式编码:
However, you might not like the idea to return an anonymous type, then I would suggest to code it this way:
var parent = (from p in db.tblParents where p.PrimaryKey == 1).Single();
var childrens = ctx.Contacts.Where(c => c.ParentID == 1 && c.IsActive == true);
foreach (var child in childrens) {
parent.tblChildren.Add(child);
}
这篇关于有条件的急切加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文