Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a C# IN operator?

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 
like image 733
Paul Michaels Avatar asked Jul 02 '10 10:07

Paul Michaels


People also ask

Why is there a slash in AC?

Senior Member. You read it as "ei-cee" (no "slash" pronounced). In terms of distinguishing between "air conditioning" and "air conditioner," I can think of an example like "Today, I bought a new air conditioner" ("conditioning" not allowed). I personally would not say "Today, I bought a new AC."

Why is it called AC?

Air conditioning, often abbreviated as A/C or AC, is the process of removing heat from an enclosed space to achieve a more comfortable interior environment (sometimes referred to as 'comfort cooling') and in some cases also strictly controlling the humidity of internal air.

What is meant by AC?

a/ c is an abbreviation for air-conditioning. Keep your windows up and the a/c on high.


2 Answers

If you wanted to write .In then you could create an extension that allows you to do that.

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...     } } 
like image 164
Andy Robinson Avatar answered Sep 20 '22 18:09

Andy Robinson


List.Contains() is I think what you're looking for. C# has in keyword and not an operator which serves completely different purpose then what you're referring in SQL.

There are two ways you can use in keyword in C#. Assume you have a string[] or List in C#.

        string[] names; //assume there are some names;          //find all names that start with "a"         var results = from str in names                       where str.StartsWith("a")                       select str;          //iterate through all names in results and print         foreach (string name in results)         {             Console.WriteLine(name);         } 

Referring your edit, I'd put your code this way to do what you need.

        int myValue = 1;         List<int> checkValues = new List<int> { 1, 2, 3 };          if (checkValues.Contains(myValue))             // Do something  
like image 20
this. __curious_geek Avatar answered Sep 21 '22 18:09

this. __curious_geek