Is there a C# IN operator?

后端 未结 14 1841
说谎
说谎 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:18

    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 checkValues = new List { 1, 2, 3 };
    
            if (checkValues.Contains(myValue))
                // Do something 
    

提交回复
热议问题