How to use the IN clause in Lambda?

Asked

Viewed 184 times

4

A doubt arose that I thought would be easy, but the content I found could not understand.

Well, here’s the thing: I have the tables

"Client"

and

"Errosproducao_client"

where they relate form 1 to N, ie 1 error can affect N Customers.

So I need to do in lambda exactly the SQL query below:

select * from Cliente 
where Id_Cliente in (select CodCliente 
                     from ErrosProducao_Cliente 
                     where codErro = 1)

I know I can solve this problem with a Join, however would like to find out how is done using IN.

Unlike the question quoted below in the answer my problem wanted to realize the IN directly through a subSelect.

  • 3

    http://answall.com/questions/18909/cl%C3%A1usula-in-com-lambda

  • Make a select of the customer codes within an array, and look at the gold link. However the performance with Join is better by what I’ve read.

  • Can you give me an example of how Join would look in this case?

2 answers

3


Do something like this.

using (var ctx = new stackoverflowEntities())
{

    var qrInSubCategoria = ctx.Cliente
        .Where(C => ctx.ErrosProducao_Cliente.Any(EC => EC.CodCliente== C.CodCliente && C.codErro == 1));
}

Or

using (var ctx = new stackoverflowEntities())
{
    var listint = ctx.ErrosProducao_Cliente
        .Where(EC => EC.codErro == 1)
        .Select(EC => EC.CodCliente)
        .ToList();

    var qrInSubCategoria = ctx.Cliente
        .Where(C => listint.Contains(C.CodCliente));

}

Or still using . Distinct() in your client code.

using (var ctx = new stackoverflowEntities())
{
    var listint = ctx.ErrosProducao_Cliente
        .Where(EC => EC.codErro == 1)
        .Select(EC => EC.CodCliente)
        .Distinct()
        .ToList();

    var qrInSubCategoria = ctx.Cliente
        .Where(C => listint.Contains(C.CodCliente));

}
  • Brawl partner!!!

  • Got very good the solution could have used a Distinct(), not to repeat Client code

  • @GOKUSSJ4, sorry but, you are mistaken.

2

// pegar os códigos do clientes e jogar na lista
var codigos = context.ErrosProducao_Cliente.Where(e => e.codErro == 1).Select(e => e.CodCliente).ToList();
// aqui seleciona os clientes que contenham na lista gerada acima
var clientes = context.Clientes.Any(c => codigos.Contains(c.codCliente)).ToList();
  • Thank you very much partner!!!

  • You’re welcome. You can only mark an answer as accepted, okay? You had marked mine and then scored another one, no problem, mark the one that attended best.

  • 2

    Got very good the solution could have used a Distinct(), not to repeat Client code

  • 1

    Yes, but in the past SQL there was no. I did to the letter what was asked hehe

  • 2

    Good tip to the next readers Virgilio, in my case I do not allow the registration of these tables include two clients for the same error.

  • Yes, of course @Murilo, I even voted in favor, I just put another cherry on the cake, since this would be an optimization in user code.

Show 1 more comment

Browser other questions tagged

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