Clause in with lambda

4

Is it possible to have an IN clause with lambda expression? Type:

context.tabela.Where(a => a.meu_campo_id_tipo_inteiro....)

let's say an in like this: (2,3,4,5) How do I put on the dots?

    
asked by anonymous 02.06.2014 / 17:41

2 answers

7

More or less. The way to do it is like this:

var inteirosPraAchar = new List<int> {2, 3, 4, 5};
context.tabela.Where(a => inteirosPraAchar.Contains(a.meu_campo_id_tipo_inteiro));
    
02.06.2014 / 17:48
4

An alternative to the method in the @Cypsy answer:

List<int> inteirosParaAchar = new List<int> { 1, 2, 3, 4 };
context.Where(i => inteirosParaAchar.Any(i1 => i1 == i));

For the reasons stated in this response (for flexibility), using Any makes comparison more flexible and can be extended to include more comparison elements.

Excerpt from answer:

  

Finally, .Any () due to the delegate, is more flexible than the .Contains () that only accepts an object.

    
02.06.2014 / 18:39