Is there a C# IN operator?

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

    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...
        }
    }
    
    0 讨论(0)
  • 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<int> checkValues = new List<int> { 1, 2, 3 };
    
            if (checkValues.Contains(myValue))
                // Do something 
    
    0 讨论(0)
  • 2020-12-08 04:22

    You usually use the Contains method of a collection.

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

    I hope it helps.

    0 讨论(0)
  • 2020-12-08 04:24

    You can do this:

    var x = 99; // searched value
    
    if (new[] {1,2,3,99}.Contains(x))
    {
       // do something
    }
    
    0 讨论(0)
  • 2020-12-08 04:26

    For digits from 0 to 9:

    "123".Contains(myValue)
    

    For any other Stuff:

    "|1|2|3|".Contains("|" + myValue + "|")
    
    0 讨论(0)
  • 2020-12-08 04:27

    You can write an extension. I wrote one time ago, for making code like

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

    more readable with an extention s.t. one was able to write

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

    Here's 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 (i.e. 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 specific to my needs at that time, but you may adapt and modify it to match more different types.

    0 讨论(0)
提交回复
热议问题