有一个C#IN运算符吗?

在SQL中,可以使用以下语法:

SELECT * FROM MY_TABLE WHERE VALUE_1 IN (1, 2, 3) 

在C#中有一个等价物吗? IDE似乎认识到“在”作为关键字,但我似乎无法find任何信息。

那么,是否有可能做如下的事情:

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

代替

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

List.Contains()是我想你在找什么。 C#中有keyword而不是一个operator完全不同的目的然后你在SQL中引用。

有两种方法可以在C#中使用关键字。 假设你在C#中有一个string[]或List。

  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); } 

引用你的编辑,我会把你的代码这样做,以满足你的需求。

  int myValue = 1; List<int> checkValues = new List<int> { 1, 2, 3 }; if (checkValues.Contains(myValue)) // Do something 

如果你想写。那么你可以创build一个扩展,允许你这样做。

 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... } } 

你可以这样做:

 var x = 99; // searched value if (new[] {1,2,3,99}.Contains(x)) { // do something } 

在C#中没有“in”运算符,“in”关键字只与“foreach(… in …)”或“from … in …”一起使用。

您的SQL查询的LINQ等价物将是:

 List<int> list = new List<int> { 1, 2, 3 }; var query = from row in my_table where list.Contains(row.value1) select row; 

您通常使用集合的Contains方法。

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

我希望它有帮助。

我同意实现In运算符的最佳方法是使用扩展方法。 我做了一些不同的事情:

 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; } 

不同之处在于,您不必在每个值周围加上引号,而只需input逗号分隔值的整个集合,这样更容易input:

 bool result = MyString.In("Val1,Val2,Val3"); 

重复: LINQ to SQL和不在

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

要么

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

在LINQ to SQL中IN和NOT IN查询的等价物是这样的:

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

和这个:

 List<int> validValues = new List<int>() { 1, 2}; var qry = from item in dataContext.TableName where !validValues.Contains(item.FieldName) select item; 

你可以写一个扩展名。 我前一段时间写了一些代码

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

更可读的一个扩展st能够写

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

代码如下:

 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 } } } 

这是当时我特别需要的一个,但你可以适应和修改它以匹配更多不同的types。

对于从0到9的数字:

 "123".Contains(myValue) 

对于其他东西:

 "|1|2|3|".Contains("|" + myValue + "|") 

为你更新的问题

利用开关

 switch (myvalue) { case 1: case 2: case 3: // your code gose here break; } 

运算符中没有查找集合中的值,而是集合的方法,称为Contains

最具扩展性的解决scheme是使用HashSet作为集合。 在HashSet检查值接近O(1)操作,而在O(n)操作的List中进行操作。 这意味着你可以在HashSet打包很多值,而且速度还是很快,而在List查找值越慢,获得的值越多。

例:

 var set = new HashSet<int>(); set.Add(1); set.Add(2); set.Add(3); var result = items.Select(i => set.Contains(i.value)); 

C#中的in关键字用于foreach语句和LINQ查询expression式。 在C#本身中,运算符中没有SQL的function,但是LINQ提供了与Contains()类似的function。

 var list = {1, 2, 3} var filtered = ( from item in items where list.Contains(item) select item).ToArray(). 
Interesting Posts