Equivalent of IN () of Sql or InList () of VFP in C #

4

Good morning, I have to search through a FOREACH if a capo contains any of the chains. Example

  foreach (var item in Clientes.Listado)
                {
                    if (item.Nombre.Contains("Juan") || item.Nombre.Contains("Carlos") || item.Nombre.Contains("Pedro")) 
                    {
                        ...Codigo
                    }
                }

I would like to change item.Nombre.Contains("Juan") || item.Nombre.Contains("Carlos") || item.Nombre.Contains("Pedro") For something more Prolijo like in FoxPro that would be

if INLIST(item.Nombre,"Juan","Carlos","Pedro")
    
asked by Ariel Octavio D'Alfeo 04.01.2018 в 20:19
source

1 answer

3

You can simply put them into an array and try using .Any() if the item.Nombre contains at least one of the values in the array.

if (new [] { "Juan", "Carlos", "Pedro" }.Any(x => item.Nombre.Contains(x))) 
{
    //...
}
    
answered by 04.01.2018 / 20:24
source