Home > OS >  How to get a list of records based on an informed enumerated list in a 1 to many relationship - EF C
How to get a list of records based on an informed enumerated list in a 1 to many relationship - EF C

Time:10-23

I need to pass an enumerated list and get only the records that have the "Application" field equivalent to the one in the enumerated list, according to the sql described below:

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;
        
}

How to do this? Thanks! :)

CodePudding user response:

It seems like you are looking for the .Contains() method.

So in your case it would be something like:

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

Also please notice that if you are not about to use FormaContatoAplicacoes later in your flow, I see no reason to use the .Include() here.

  • Related