Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if statements matching multiple values

Any easier way to write this if statement?

if (value==1 || value==2)

For example... in SQL you can say where value in (1,2) instead of where value=1 or value=2.

I'm looking for something that would work with any basic type... string, int, etc.

like image 606
Ricky Avatar asked Oct 11 '10 14:10

Ricky


People also ask

How do you do an if statement with multiple conditions?

When you combine each one of them with an IF statement, they read like this: AND – =IF(AND(Something is True, Something else is True), Value if True, Value if False) OR – =IF(OR(Something is True, Something else is True), Value if True, Value if False) NOT – =IF(NOT(Something is True), Value if True, Value if False)

Can IF statement have 2 conditions Excel?

The IF function allows you to make a logical comparison between a value and what you expect by testing for a condition and returning a result if True or False. So an IF statement can have two results. The first result is if your comparison is True, the second if your comparison is False.

Can Excel find multiple matches?

Excel allows us to return multiple match result of a lookup using the TEXTJOIN and IF functions. This step by step tutorial will assist all levels of Excel users to learn how to return multiple match results in Excel.


1 Answers

How about:

if (new[] {1, 2}.Contains(value)) 

It's a hack though :)

Or if you don't mind creating your own extension method, you can create the following:

public static bool In<T>(this T obj, params T[] args) {     return args.Contains(obj); } 

And you can use it like this:

if (1.In(1, 2)) 

:)

like image 64
Amry Avatar answered Sep 19 '22 03:09

Amry