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
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."
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.
a/ c is an abbreviation for air-conditioning. Keep your windows up and the a/c on high.
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... } }
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With