有C#IN运算符吗?

Pau*_*els 80 c# sql operators in-operator

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

SELECT *
FROM MY_TABLE
WHERE VALUE_1 IN (1, 2, 3)
Run Code Online (Sandbox Code Playgroud)

C#中有等价物吗?IDE似乎将"in"识别为关键字,但我似乎无法在其上找到任何信息.

那么,是否可以执行以下操作:

int myValue = 1;
if (myValue in (1, 2, 3))
    // Do something
Run Code Online (Sandbox Code Playgroud)

代替

int myValue = 1;
if (myValue == 1 || myValue == 2 || myValue == 3)
    // Do something
Run Code Online (Sandbox Code Playgroud)

And*_*son 109

如果你想写.那么你可以创建一个允许你这样做的扩展.

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...
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 相同版本,不会引发异常:`return items?.Contains(item) ?? 假;` (4认同)
  • 只记得添加 using System.Linq; (3认同)
  • In 比包含更好阅读......更容易理解 (2认同)

thi*_*eek 82

List.Contains()我觉得你在寻找什么.C#有in keyword没有与operator你在SQL中引用的完全不同的目的.

有两种方法可以in在C#中使用关键字.假设您在C#中有一个字符串[]或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);
        }
Run Code Online (Sandbox Code Playgroud)

参考你的编辑,我会用你的代码来做你需要的.

        int myValue = 1;
        List<int> checkValues = new List<int> { 1, 2, 3 };

        if (checkValues.Contains(myValue))
            // Do something 
Run Code Online (Sandbox Code Playgroud)

  • 人们看到SQL并立即跳转到LINQ,但这个简单的功能可能正是他想要的 (4认同)

JwJ*_*efy 23

你可以这样做:

var x = 99; // searched value

if (new[] {1,2,3,99}.Contains(x))
{
   // do something
}
Run Code Online (Sandbox Code Playgroud)

  • 我更喜欢这个答案而不是更高投票的答案,因为想要做 IN 而不是重复的相等性检查的重点是降低代码复杂性,这很好,简短而简单! (3认同)

luc*_*uke 7

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

myCollection.Where(p => Enumerable.Range(1,3).Contains(p));
Run Code Online (Sandbox Code Playgroud)

我希望它有所帮助.


Dan*_*iel 6

C#中没有"in"运算符,"in"关键字仅用于"foreach(... in ...)"或"from ... in ...".

LINQ等效的SQL查询将是:

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


小智 5

我同意实现 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;
}
Run Code Online (Sandbox Code Playgroud)

不同之处在于,您不必在每个值两边加上引号,只需将整组逗号分隔的值加上引号,这样更容易键入:

bool result = MyString.In("Val1,Val2,Val3");
Run Code Online (Sandbox Code Playgroud)