Is there a C # IN statement?

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 #? It seems that the IDE recognizes "in" as a keyword, but I seem to be unable to find any information about this.

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

 int myValue = 1; if (myValue in (1, 2, 3)) // Do something 

Instead

 int myValue = 1; if (myValue == 1 || myValue == 2 || myValue == 3) // Do something 
+64
operators c # sql in-operator
Jul 02 '10 at 10:44
source share
12 answers

List.Contains() I think what you are looking for. C # has an in keyword , not an operator , which serves completely different purposes than what you mean in SQL.

There are two ways to use the in keyword in C #. Suppose 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 to your editing, I would put your code in such a way as to do what you need.

  int myValue = 1; List<int> checkValues = new List<int> { 1, 2, 3 }; if (checkValues.Contains(myValue)) // Do something 
+68
Jul 02 2018-10-02T00:
source share

If you want to write. In this case, you can create an extension that allows you to do 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... } } 
+80
Jul 02 '10 at 11:13
source share

You can do it:

 var x = 99; // searched value if (new[] {1,2,3,99}.Contains(x)) { // do something } 
+16
Aug 30 '13 at 19:42
source share

In C # there is no "in" operator, the "in" keyword is used only with "foreach (... in ...)" or "from ... in ...".

LINQ equivalent of your SQL query:

 List<int> list = new List<int> { 1, 2, 3 }; var query = from row in my_table where list.Contains(row.value1) select row; 
+6
Jul 02 '10 at
source share

Usually you use the Contains collection method.

 myCollection.Where(p => Enumerable.Range(1,3).Contains(p)); 

Hope this helps.

+6
Jul 02 '10 at 10:52
source share

I agree that the best way to implement the In operator is through an extension method. I did it a little differently:

 public static bool In(this string str, string CommaDelimintedStringSet) { string[] Values = CommaDelimintedStringSet.Split(new char[] { ',' }); foreach (string V in Values) { if (str == V) return true; } return false; } 

The difference is that you do not need to put quotation marks around each value, but only the entire set of comma-separated values โ€‹โ€‹that are easier to enter:

 bool result = MyString.In("Val1,Val2,Val3"); 
+5
Sep 24 '12 at 19:34
source share

Duplicate: LINQ to SQL, not in

 select * from table where fieldname in ('val1', 'val2') 

or

 select * from table where fieldname not in (1, 2) 

The equivalent of IN and NOT IN queries in LINQ to SQL would be something like this:

 List<string> validValues = new List<string>() { "val1", "val2"}; var qry = from item in dataContext.TableName where validValues.Contains(item.FieldName) select item; 

and this:

 List<int> validValues = new List<int>() { 1, 2}; var qry = from item in dataContext.TableName where !validValues.Contains(item.FieldName) select item; 
+4
Jul 02 '10 at 10:50
source share

You can write an extension. I wrote once to create code like

 if(someObject.stringPropertyX.Equals("abc") || someObject.stringPropertyX.Equals("def") || ....){ //do something ... }else{ //do something other... .... } 

more readable with the extension st one could write

 if(someObject.stringPropertyX.In("abc", "def",...,"xyz"){ //do something ... }else{ //do something other... .... } 

Here is the code :

 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Some.Namespace.Extenders { public static class StringExtender { /// <summary> /// Evaluates whether the String is contained in AT LEAST one of the passed values (ie similar to the "in" SQL clause) /// </summary> /// <param name="thisString"></param> /// <param name="values">list of strings used for comparison</param> /// <returns><c>true</c> if the string is contained in AT LEAST one of the passed values</returns> public static bool In(this String thisString, params string[] values) { foreach (string val in values) { if (thisString.Equals(val, StringComparison.InvariantCultureIgnoreCase)) return true; } return false; //no occurence found } } } 

This is the one that fits my needs at the time, but you can adapt and modify it to fit other types.

+2
Jul 02 '10 at 11:10
source share

For numbers from 0 to 9:

 "123".Contains(myValue) 

For any other material:

 "|1|2|3|".Contains("|" + myValue + "|") 
+2
Feb 26 '14 at 2:57
source share

For an updated question

use switch

 switch (myvalue) { case 1: case 2: case 3: // your code gose here break; } 
+1
Jul 02 2018-10-02T00:
source share

The statement that searches for the value in the collection does not, instead it is a collection method called Contains .

The most scalable solution is to use a HashSet as a collection. Checking a value in a HashSet is close to an O (1) operation, compared to doing it in a List , where it is an O (n) operation. This means that you can pack many values โ€‹โ€‹in a HashSet and still fast, while finding the value in the List becomes slower the more values โ€‹โ€‹you have.

Example:

 var set = new HashSet<int>(); set.Add(1); set.Add(2); set.Add(3); var result = items.Select(i => set.Contains(i.value)); 
+1
Jul 02 '10 at 11:12
source share

The in keyword in C # refers to the foreach and LINQ query expressions. The SQL in functionality in C # does not exist as such, but LINQ offers similar functionality with Contains() .

 var list = {1, 2, 3} var filtered = ( from item in items where list.Contains(item) select item).ToArray(). 
0
Jul 02 2018-10-02T00:
source share



All Articles