What is the option in C# having capability of same like 'IN' in SQl

I want to search more than one Condition in an if Conditional block same like IN operator does in SQL.

public class Check{
int [] arr={1,2,5,9,7,11,89};
for(int i=0;i<arr.length;i++)
{
  if(arr[i]==1||arr[i]==5||arr[i]==7||arr[i]==89)
  {
    Console.WriteLine("The No Is Found.");
  }
}

Is here any solution for this kind of result.

if(arr[i] in(1,5,7,89)
  {
    Console.WriteLine("The No Is Found.");
  }
Jon Skeet
people
quotationmark

There's nothing in C# as a language that's equivalent to IN, no... but you can achieve a similar effect easily.

The simplest approach is to probably to use System.Linq and Contains against an array:

using System;
using System.Linq;

public class Check{

    static void Main()
    {
        int[] candidates = {1, 2, 5, 9, 7, 11, 89};
        // This is the members of the "in" clause - the
        // the values you're trying to check against
        int[] targets = { 1, 5, 7, 89 };
        foreach (int candidate in candidates)
        {
            Console.WriteLine(
                targets.Contains(candidate) ?
                $"{candidate} is in targets" :
                $"{candidate} is not in targets");
        }
    }
}

Alternatively, you could use a HashSet<int> - that would be more efficient if you had a large number of targets:

using System;
using System.Collections.Generic;

public class Check{

    static void Main()
    {
        int[] candidates = {1, 2, 5, 9, 7, 11, 89};
        var targets = new HashSet<int> { 1, 5, 7, 89 };
        foreach (int candidate in candidates)
        {
            Console.WriteLine(
                targets.Contains(candidate) ?
                $"{candidate} is in targets" :
                $"{candidate} is not in targets");
        }
    }
}

people

See more on this question at Stackoverflow