Is there a C# IN operator?

后端 未结 14 1838
说谎
说谎 2020-12-08 03:51

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

相关标签:
14条回答
  • 2020-12-08 04:28

    For your updated question, you could also use a switch-statement.

    switch (myvalue)
    {
       case 1:
       case 2:
       case 3: 
          // your code goes here
      break;
    }
    
    0 讨论(0)
  • 2020-12-08 04:28

    The in keyword in C# is for the foreach statement and for LINQ query expressions. There is no functionality equivalent to SQL's in operator in C# per se, 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 讨论(0)
提交回复
热议问题