如何获得条件包括子实体及其相关实体

时间:2012-11-05 02:18:39

标签: linq entity

以下代码适用于我:

var query0 = DB.Plan
    .Include("UserCreate")
    .Include("UserUpdate")
    .Include("PlanTemplate")
    .Select(p => new
            {
                plan = p,
                planItems = p.PlanItems.Where(pi => pi.IsCommited == true && pi.IsLocked ==    false)
            });
var query2 = query0.AsEnumerable().Where(m => m.planItems.Count() > 0).Select(m => m.plan);

但是planItems与我想要的其他实体相关,以下代码对我不起作用:

var query0 = DB.WorkEffortPlan
.Include("UserCreate")
.Include("UserUpdate")
.Include("PlanTemplate")
.Select(p => new
{
    plan=p,
    planItems = DB.PlanItem
    .Include("UserResponsiblePerson")
    .Include("TemplateItem")
    .Include("ItemState")
    .Where(pi => pi.PlanID == p.PlanID && pi.IsCommited == true && pi.IsLocked == false)
});
var query2 = query0.AsEnumerable().Where(m => m.planItems.Count() > 0).Select(m => m.plan);

vs编译器运行正常,但是当执行此代码时,我在query0中得到NullReferenceException。

是否有任何linq解决方案来过滤子实体及其相关实体?

0 个答案:

没有答案