C# C语言中有什么选项具有与'相同的功能;在';在SQL中?

C# C语言中有什么选项具有与'相同的功能;在';在SQL中?,c#,console-application,C#,Console Application,我想在if条件块中搜索多个条件-与SQL中的in运算符相同 public class Check{ int [] arr = {1, 2, 5, 9, 7, 11, 89}; for (int i=0; i<arr.length; i++) { if(arr[i]==1||arr[i]==5||arr[i]==7||arr[i]==89) { Console.WriteLine("The number was found."

我想在
if
条件块中搜索多个条件-与SQL中的in运算符相同

public class Check{

    int [] arr = {1, 2, 5, 9, 7, 11, 89};
    for (int i=0; i<arr.length; i++)
    {
      if(arr[i]==1||arr[i]==5||arr[i]==7||arr[i]==89)
      {
        Console.WriteLine("The number was found.");
      }
    }
在C语言中没有任何东西可以与中的相媲美,不。。。但是你可以很容易地达到类似的效果

最简单的方法可能是对数组使用
System.Linq
Contains

using System;
using System.Linq;

public class Check{

    static void Main()
    {
        int[] candidates = {1, 2, 5, 9, 7, 11, 89};
        // This is the members of the "in" clause - the
        // the values you're trying to check against
        int[] targets = { 1, 5, 7, 89 };
        foreach (int candidate in candidates)
        {
            Console.WriteLine(
                targets.Contains(candidate) ?
                $"{candidate} is in targets" :
                $"{candidate} is not in targets");
        }
    }
}
或者,您可以使用
哈希集
——如果您有大量目标,这将更有效:

using System;
using System.Collections.Generic;

public class Check{

    static void Main()
    {
        int[] candidates = {1, 2, 5, 9, 7, 11, 89};
        var targets = new HashSet<int> { 1, 5, 7, 89 };
        foreach (int candidate in candidates)
        {
            Console.WriteLine(
                targets.Contains(candidate) ?
                $"{candidate} is in targets" :
                $"{candidate} is not in targets");
        }
    }
}
使用系统;
使用System.Collections.Generic;
公共类检查{
静态void Main()
{
int[]候选人={1,2,5,9,7,11,89};
var targets=newhashset{1,5,7,89};
foreach(候选人中的int候选人)
{
控制台写入线(
目标。包含(候选)?
$“{candidate}在目标中”:
$“{candidate}不在目标中”);
}
}
}
作为一种语言,C语言中没有任何东西可以等同于
中的
,不。。。但是你可以很容易地达到类似的效果

最简单的方法可能是对数组使用
System.Linq
Contains

using System;
using System.Linq;

public class Check{

    static void Main()
    {
        int[] candidates = {1, 2, 5, 9, 7, 11, 89};
        // This is the members of the "in" clause - the
        // the values you're trying to check against
        int[] targets = { 1, 5, 7, 89 };
        foreach (int candidate in candidates)
        {
            Console.WriteLine(
                targets.Contains(candidate) ?
                $"{candidate} is in targets" :
                $"{candidate} is not in targets");
        }
    }
}
或者,您可以使用
哈希集
——如果您有大量目标,这将更有效:

using System;
using System.Collections.Generic;

public class Check{

    static void Main()
    {
        int[] candidates = {1, 2, 5, 9, 7, 11, 89};
        var targets = new HashSet<int> { 1, 5, 7, 89 };
        foreach (int candidate in candidates)
        {
            Console.WriteLine(
                targets.Contains(candidate) ?
                $"{candidate} is in targets" :
                $"{candidate} is not in targets");
        }
    }
}
使用系统;
使用System.Collections.Generic;
公共类检查{
静态void Main()
{
int[]候选人={1,2,5,9,7,11,89};
var targets=newhashset{1,5,7,89};
foreach(候选人中的int候选人)
{
控制台写入线(
目标。包含(候选)?
$“{candidate}在目标中”:
$“{candidate}不在目标中”);
}
}
}

许多内置容器类已经有一个名为
Contains()的内置方法来完成此任务。如果你创建了自己的自定义容器类,你也应该在其中包含一个类似的方法。这会搜索条件语句吗?如果你有一个包含多个条件的C#容器类对象,那么它会搜索一个条件,是的。例如,如果您创建
List x=new List{cond1,cond2,cond3,cond4,cond5}的实例
然后
x.Contains(cond1)
将返回true,而
x.Contains(cond12)
将返回false;我要试试这个……谢谢你的帮助。你必须创建一个类来保存一个“条件”-但是你需要定义/构造它。如果只是“任何计算结果为true或false的C#表达式”,那么您需要研究C#lambda表达式:许多内置容器类已经有一个名为
Contains()
的内置方法来实现这一点。如果你创建了自己的自定义容器类,你也应该在其中包含一个类似的方法。这会搜索条件语句吗?如果你有一个包含多个条件的C#容器类对象,那么它会搜索一个条件,是的。例如,如果您创建
List x=new List{cond1,cond2,cond3,cond4,cond5}的实例
然后
x.Contains(cond1)
将返回true,而
x.Contains(cond12)
将返回false;我要试试这个……谢谢你的帮助。你必须创建一个类来保存一个“条件”-但是你需要定义/构造它。如果只是“任何计算结果为true或false的C#表达式”,则需要研究C#lambda表达式: