Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I compress an if statement using linq in C#?

Tags:

c#

linq

In sql, I can make an if statement like the following If MY_STATE in (1,2,3,4)

In C# I have to type if(MY_STATE == STATE.CT || MY_STATE == STATE.MA || MY_STATE == STATE.VA || MY_STATE == STATE.RI)

This is obviously more clunky. Can I use LINQ to mimic the "in" statement of sql?

I'm looking for something like if(MY_STATE in (STATE.CT, STATE.MA, STATE.VA, STATE.RI))

like image 560
diadem Avatar asked Nov 27 '22 12:11

diadem


1 Answers

if (new [] {State.CT, State.MA, State.VA, State.RI}.Contains(myState)) {
  // There you go
}
like image 107
Benjamin Podszun Avatar answered Dec 16 '22 03:12

Benjamin Podszun