Is there a C# IN operator?

不打扰是莪最后的温柔 提交于 2019-11-28 05:09:53

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

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 

You can do this:

var x = 99; // searched value

if (new[] {1,2,3,99}.Contains(x))
{
   // do something
}
luckyluke

You usually use the Contains method of a collection.

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

I hope it helps.

There's no "in" operator in C#, the "in" keyword is used only with "foreach (... in ...)" or "from ... in ...".

The LINQ equivalent of your SQL query would be:

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

Duplicate of : LINQ to SQL in and 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; 
Chuck Bevitt

I agree the best way to implement the In operator is with 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 don't have to put quotes around each value, only the entire set of comma delimited values, which is easier to type:

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

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.

Pranay Rana

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

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

There is no in operator that looks for a value in a collection, instead it's a method of the collection, called Contains.

The most scalable solution is to use a HashSet as the collection. Checking for 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. That means that you can pack a lot of values in a HashSet and it's still fast, while looking for a value in a List gets 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));
user3354025

For digits from 0 to 9:

"123".Contains(myValue)

For any other Stuff:

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

Common, LINQ way more powerful:

var list = new List<string> { "Tomato", "Orange", "Mango"};
var query = from i in my_table
            from v in list
            where i.Name.StartsWith(v)
            select i;

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().
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!