2015-11-18 29 views
1

我在这里有一个错误代码,因为我无法检查字符串是否等于string []。C#Linq - 如果输入不等于任何字符串[]

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[] options。我真的想要一个整洁的模板,我可以使用其余的方法。任何人都可以帮忙吗?

+2

我相信'setting.Any'应该是'option.Any'如果我明白你的问题的正确 –

+2

可能的复制[检查是否值是一个数组(C#)](HTTP://计算器。 com/questions/13257458/check-if-a-value-is-an-an-array-c) –

+0

是的,那是一个错字。无论如何,抱歉的重复。我无法相信在发布之前我找不到该线索。 –

回答

4

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

改变你的条件要

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

或者另一种选择

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

请与下面的代码片段尝试。

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

     //I wanna check if input is equal to any of the string array 
     if (!setting.Contains(input)) 
     { 
      //MessageBox.Show("Invalid Input"); 
     } 

     else 
     { 
      //Proceed with other stuffs 
     } 
    }