Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C中的运算符:是否有类似于;在;SQL Server中的运算符?_C#_Operators_Code Behind - Fatal编程技术网

C# C中的运算符:是否有类似于;在;SQL Server中的运算符?

C# C中的运算符:是否有类似于;在;SQL Server中的运算符?,c#,operators,code-behind,C#,Operators,Code Behind,我在一个C代码中得到了这条语句,我正在检查我的变量是否等于一个特定的数字,如果是,它将处理一些代码: try { int TaskID = Convert.ToInt32(ddlTask.SelectedValue); if (TaskID == 157) { //lblAccountName.Text = (DT1["CUST_NM"].ToString()); Label2.Text = (DT1["CUST_NM"].ToStr

我在一个C代码中得到了这条语句,我正在检查我的变量是否等于一个特定的数字,如果是,它将处理一些代码:

try
{
    int TaskID = Convert.ToInt32(ddlTask.SelectedValue);

    if (TaskID == 157)
    {
         //lblAccountName.Text = (DT1["CUST_NM"].ToString());
         Label2.Text = (DT1["CUST_NM"].ToString());
         //lblAccountName.Visible = true;
         TBAccountNum.Text = (DT1["CUST_NUM"].ToString());
         TBAccountNum.Visible = true;
     }
}
catch (Exception ae)
{
     Response.Write(ae.Message);
}
工作完美。然而,现在我想在“如果”列表中添加一些其他数字。在SQL Server中,它将类似于:

if (TaskID IN (157, 158, 159, 160, 162, 165))
在C#中有什么方法可以做到这一点吗?我仍在学习中,如果这有点简单,我深表歉意。

您可以将项目集合(例如数组)与LINQ一起使用:

new[] { 157, 158, 159, 160, 162, 165 }.Contains(TaskID)
只要写下:

if ( new int[]{157, 158, 159, 160, 162, 165}.Contains(TaskID))
    ....
不要忘记添加以下参考:

using System.Linq;

您还可以使用
switch
语句,这将允许其他可能性:

switch (taskId)
{
    case 175:
    case 176:
    case 177:
        // code for any of the values above
        break;

    case 200:
        // yet another possibility
        break;

    default:
        // any other value
        break;
}

仅供参考,您不需要指定可以推断的数组类型(
new[]{…
vs
new int[]{…
)。这和svick的答案都很完美,我检查了这一个,因为它更完整。谢谢!