How to use SQL "in" in a lambda (Entity) C#?

Asked

Viewed 69 times

0

I have a repository that represents my table in the database

    var RelacaoPessoas = _repositoryPessoas.GetFiltered(i => i.IdProcesso == idprocesso && i.Banco == numeroDoBanco && i.CpfCnpj.Contains(cpfCnpj)).ToList();

I need to rescue the records from a list of Cpf’s, in sql would be something like this:

    select * from Pessoas where IdProcesso = @idprocesso and Banco = @numeroBanco and CpfCnpj in (<lista de cpfs aqui>)

However, I don’t know what the equivalent function of "in" would be. I know that "Contains" accepts string, I tried to pass this list as a comma-separated string like this: "cpf1,cpf2,cpf3..." but it didn’t work.

Can someone help me?

  • syntax apparently is ok, Contaisn is the equivalent of in, but some data from your query must be returning false already checked if idprocess and numeroDoBanco have expected values in the database ?

  • Marcos, Contains would be equivalent to the "like" of sql, so as I was going to stop "cpf1,cpf2,cpf3...", in sql it would be the same as "select * from table Where Cpf like 'cpf1,cpf2,cpf3...' ". For this reason I was not returning anything

1 answer

0


If you have a list of Cpfs you can do something more or less like this:

Imagine that your list of filter Cpfs is stored in the following variable:

var listCpfParaFiltro = new List<string>();

When making the filter encode like this:

var RelacaoPessoas = _repositoryPessoas.GetFiltered(i => i.IdProcesso == idprocesso && i.Banco == numeroDoBanco && listCpfParaFiltro.Contains(i.CpfCnpj)).ToList();
  • 1

    Thank you Thiago. I had already created a string list, only I put this list inside Contains(), IE, the syntax was the other way around. Thanks even for the help, it worked and the query returned as expected

Browser other questions tagged

You are not signed in. Login or sign up in order to post.