If x Not In (a,b,c,…) equivalent of T-SQL in VB.NET

I have seen many hits on my blog with similar search terms and also the question being asked in various forums. The answer is really very simple.
Consider the following block in SQL:

IF TestValue IN (Value1, Value2, Value3, Value4, Value5)
BEGIN
    -- do something here

END

The VB.NET equivalent of the above would be:

Select Case TestValue
    Case Value1, Value2, Value3, Value4, Value5
        ' do something here

End Select

And the negated version isn’t any difficult either.


SQL Version:

IF TestValue NOT IN (Value1, Value2, Value3, Value4, Value5)
BEGIN
    -- do something here

END

VB.NET Version:

Select Case TestValue
    Case Value1, Value2, Value3, Value4, Value5
    Case Else
        ' do something here

End Select

There could be many other ways to do this. But I find this one a lot more handy and also easy to read.

Enjoy!Smile

Advertisements

2 Responses to “If x Not In (a,b,c,…) equivalent of T-SQL in VB.NET”

  1. Turban Says:

    Excellent issues altogether, you just received a new reader. What might you suggest about your put up that you made some days in the past? Any certain?

  2. pepe Says:

    VB.NET equivalent of the above would be:

    dim myList Integer() = new Integer() {Value1, Value2, Value3, Value4, Value5 }
    if myList.contains( TestValue ) Then …


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: