C# Linq - 如果输入不等于任何字符串[]

Lir*_*Yeo 1 c# linq arrays string if-statement

我这里有一个错误代码,因为我无法检查 string 是否等于 string[]。

public void SetCh1Probe(string input)
{
    string[] option= {"1:1", "1:10", "1:100"}

    //I wanna check if input is equal to any of the string array
    if (input != option.Any(x => x == option))
    {
        MessageBox.Show("Invalid Input");
    }

    else
    {
        //Proceed with other stuffs
    }
}
Run Code Online (Sandbox Code Playgroud)

我将有大量这样的方法,每种方法都有不同的string[] options. 我真的想要一个简洁的模板,可以用于其余的方法。有人可以帮忙吗?

M.k*_*ary 5

改变你的状况

if (input != option.Any(x => x == option))
Run Code Online (Sandbox Code Playgroud)

if (!option.Any(x => x == input))
Run Code Online (Sandbox Code Playgroud)

或者另一种选择

if (option.All(x => x != input))
Run Code Online (Sandbox Code Playgroud)