无法翻译 LINQ 表达式 - 可空 Id 字段出错

The LINQ expression could not be translated - Error with Nullable Id field

在我的控制器中,我试图取回属于某个公司的所有人员记录,我正在尝试这样做:

[HttpGet]
[Route("getCompanyPeople/{companyId}")]
public ActionResult<List<CompanyPeopleDTO>> getCompanyPeople(int companyId)
{
  List<CompanyPeopleDTO> companyPeople = _context.TblPeople.Select(e => new CompanyPeopleDTO()
    {
      PersonId = e.PersonId,
      NamePrefix = e.NamePrefix,
      FirstName = e.FirstName,
      LastName = e.LastName,
      NameSuffix = e.NameSuffix,
      peopleTypeDesc = e.PeopleTypeNavigation.PeopleTypeDesc,
      contactTypeDesc = e.ContactTypeNavigation.ContactTypeDesc
    }).Where(e => e.CompanyId == companyId).ToList();

    return companyPeople;
 }

但是,在对此进行测试时,我收到一条错误消息:

System.InvalidOperationException: 'The LINQ expression 'DbSet<TblPeople>
    .LeftJoin(
        outer: DbSet<TblPeopleTypes>, 
        inner: t => EF.Property<Nullable<int>>(t, "PeopleType"), 
        outerKeySelector: t0 => EF.Property<Nullable<int>>(t0, "PeopleTypeId"), 
        innerKeySelector: (o, i) => new TransparentIdentifier<TblPeople, TblPeopleTypes>(
            Outer = o, 
            Inner = i
        ))
    .LeftJoin(
        outer: DbSet<TblContactTypes>, 
        inner: t => EF.Property<Nullable<int>>(t.Outer, "ContactType"), 
        outerKeySelector: t1 => EF.Property<Nullable<int>>(t1, "ContactTypeId"), 
        innerKeySelector: (o, i) => new TransparentIdentifier<TransparentIdentifier<TblPeople, TblPeopleTypes>, TblContactTypes>(
            Outer = o, 
            Inner = i
        ))
    .Where(t => (Nullable<int>)new CompanyPeopleDTO{ 
        PersonId = t.Outer.Outer.PersonId, 
        NamePrefix = t.Outer.Outer.NamePrefix, 
        FirstName = t.Outer.Outer.FirstName, 
        LastName = t.Outer.Outer.LastName, 
        NameSuffix = t.Outer.Outer.NameSuffix, 
        peopleTypeDesc = t.Outer.Inner.PeopleTypeDesc, 
        contactTypeDesc = t.Inner.ContactTypeDesc 
    }
    .CompanyId != null && new CompanyPeopleDTO{ 
        PersonId = t.Outer.Outer.PersonId, 
        NamePrefix = t.Outer.Outer.NamePrefix, 
        FirstName = t.Outer.Outer.FirstName, 
        LastName = t.Outer.Outer.LastName, 
        NameSuffix = t.Outer.Outer.NameSuffix, 
        peopleTypeDesc = t.Outer.Inner.PeopleTypeDesc, 
        contactTypeDesc = t.Inner.ContactTypeDesc 
    }
    .CompanyId == __companyId_0)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.'

我认为这可能是因为 companyId 可能为空。我已经尝试了解决方案 ,但它没有用 - 它给了我和以前一样的错误,而且我看得越多,我不认为我正在尝试做的事情真的与那个有关问题。

我在没有检查人员记录上的 companyId 的情况下进行了测试 - 成功 returns 所有人员记录,但显然,这不是我想要的。

试试这个:

var companyPeople = _context.TblPeople
.Where(e => e.CompanyId == companyId)
.Select(e => new CompanyPeopleDTO()
    {
      PersonId = e.PersonId,
      NamePrefix = e.NamePrefix,
      FirstName = e.FirstName,
      LastName = e.LastName,
      NameSuffix = e.NameSuffix,
      peopleTypeDesc = e.PeopleTypeNavigation.PeopleTypeDesc,
      contactTypeDesc = e.ContactTypeNavigation.ContactTypeDesc
    }).ToList();