Linq Include not working

本文关键字:working not Include Linq | 更新日期: 2023-09-27 18:12:27

我有这个linq表达式

ActionPlan
    .Include(x => x.Goal)
    .Include(x => x.Responsibles)
    .Include(x => x.Goal.EducationalPlan)
    .AsNoTracking()
    .Where(x => x.State == State.Active && x.Goal.State == State.Active && x.Goal.EducationalPlan.State == State.Active)
    .Where(x => x.Responsibles.Any(r => r.PersonId == pes_id))
    .Distinct()
    .Select(x => x.Goal)
    .ToList();

包含的教育计划不工作,为什么?其他工作正常,教育计划是一个对象而不是一个列表。

Linq Include not working

对于多个级别,您应该这样做:

ActionPlan
    .Include(x => x.Goal)
    .Include(x => x.Responsibles)
    .Include(x => x.Goal.Select(y => y.EducationalPlan))
    .AsNoTracking()
    .Where(x => x.State == State.Active && x.Goal.State == State.Active && x.Goal.EducationalPlan.State == State.Active)
    .Where(x => x.Responsibles.Any(r => r.PersonId == pes_id))
    .Distinct()
    .Select(x => x.Goal)
    .ToList();

或者试试这个:

ActionPlan
    .Include(x => x.Goal)
    .Include(x => x.Responsibles)
    .Include("Goal.EducationalPlan")
    .AsNoTracking()
    .Where(x => x.State == State.Active && x.Goal.State == State.Active && x.Goal.EducationalPlan.State == State.Active)
    .Where(x => x.Responsibles.Any(r => r.PersonId == pes_id))
    .Distinct()
    .Select(x => x.Goal)
    .ToList();