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

标签 c# arrays string linq if-statement

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

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

我会有大量这样的方法,每个方法都有不同的string[] 选项。我真的很想拥有一个可以用于其余方法的简洁模板。有人可以帮忙吗?

最佳答案

改变你的条件从

if (input != option.Any(x => x == option))

if (!option.Any(x => x == input))

或者另一种选择

if (option.All(x => x != input))

关于C# Linq - 如果输入不等于任何字符串 [],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33772838/

相关文章:

c# - 字节流中的双值传输

c# - Unity 测试设置强制排除我想要从项目中测试的所有代码

c# - 如何获取 dataGrid1_BeginningEdit 事件中数据网格单元格的值?

Java浅拷贝数组

javascript - 将字符串匹配到数组

c# - 创建单实例 WPF 应用程序的正确方法是什么?

java - 使用列表 Android Studio 从 HTMLData 获取 URL

javascript - 在 JavaScript 中动态更新嵌套对象

c - __func__ 错误

java - Java中如何将字符串放入JSON并获取值