Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple value for if-statement

Tags:

c#

list

If my statement contains two conditions:

string searchx = "some string"; 

if ((searchx.Contains("a1")) || (searchx.Contains("a2")))
{
...
}

But how to get list of values with single variable for statement?

If I got a1, a2, a3, a4, a5, a6, a7, a8, a9...

Can I do it somehow this way, seems it is wrong attempt:

var valueList = new List<string> { "a1", "a2", "a3", "a4"};

But just to explain what I want to do, so if any value exist under valueList, condition is accepted:

if (searchx.Contains(valueList)) 
{
...
}

The best if I can get multiple value return I guess or any other way to get statement with updated list of values through single variable of any other way, which can work for me this way?


1 Answers

This has worked for me:

if (valueList.Any(x => searchx.Contains(x)))
{
}

or even shorter (thanks to rajeeshmenoth)

    if(valueList.Any(searchx.Contains))
like image 172
Schmid Avatar answered Mar 03 '26 13:03

Schmid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!