Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

In SQL, you can use the following syntax:

SELECT *

FROM MY_TABLE

WHERE VALUE_1 IN (1, 2, 3)

Is there an equivalent in C#? The IDE seems to recognise "in" as a keyword, but I don't seem to be able to find any information on it.

So, is it possible to do something like the following:

int myValue = 1;

if (myValue in (1, 2, 3))

    // Do something

Instead of

int myValue = 1;

if (myValue == 1 || myValue == 2 || myValue == 3)

    // Do something

1 Answer

0 votes
by (40.7k points)

You can create an extension that allows you to write .In like this:

static class Extensions

{

   public static bool In<T>(this T item, params T[] items)

    {

        if (items == null)

            throw new ArgumentNullException("items");

        return items.Contains(item);

    }

}

class Program

{

static void Main()

    {

   int myValue = 1;

        if (myValue.In(1, 2, 3))

            // Do Somthing...

        string ds = "Bob";

        if (ds.In("andy", "joel", "matt")) 

        // Do Someting...

    }

}

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Oct 5, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
asked Jul 30, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer

Browse Categories

...