如何根据一对多关系中的知情枚举列表获取记录列表 - EF Core

How to get a list of records based on an informed enumerated list in a 1 to many relationship - EF Core

我需要传递一个枚举列表,并根据下面描述的 sql 仅获取“应用程序”字段与枚举列表中的记录相同的记录:

select * from "FormaContato" as "fc"
inner join "FormaContatoAplicacao" as "fca" ON "fc"."Id" = "fca"."FormaContatoId"
where "fca"."AplicacaoId" in (2, 1, ...)

public enum AplicacaoEnum
{
    [Description("PESSOA FÍSICA")]
    Nenhum = 1,
    [Description("PESSOA JURÍDICA")]
    ContatoTelefonico = 2
}

public IQueryable<FormaContato> GetAllByAplicacao(List<AplicacaoEnum> aplicacaoList = null)
{
    var data = DbSet.
        Include(x => x.FormaContatoAplicacoes)
        .Where(x=> x.FormaContatoAplicacoes.Aplicacao == aplicacaoList ????????? Here.)
        .AsNoTracking();

    return data;
        
}

如何做到这一点? 谢谢! :)

您似乎在寻找 .Contains() 方法。

所以在你的情况下它会是这样的:

var data = DbSet
     .Include(x => x.FormaContatoAplicacoes)
     .Where(x=> aplicacaoList.Contains(x.FormaContatoAplicacoes.Aplicacao))
     .AsNoTracking();

另请注意,如果您稍后不打算在流程中使用 FormaContatoAplicacoes,我认为没有理由在此处使用 .Include()