5

In TSQL..

IF MyVal IN (1, 2, 3, 4, 14) BEGIN ... END 

Is there a way to do this in VB.NET?

Is it possible to check for the existence of an integer in a set of integers inline?

Such as:

If MyVal in (1, 2, 3, 4, 14) Then ... End If 

2 Answers 2

6

Arrays are an implementation of IEnumerable so with the System.Linq import a shorthand version of Tim Schmelter's answer would be:

{1,2,3,4,14}.Contains(MyVal) 

Arrays also have an explicit implementation of IList.Contains, so without LINQ a perhaps less elegant alternative is:

DirectCast({1,2,3,4,14}, IList).Contains(MyVal) 
Sign up to request clarification or add additional context in comments.

Comments

3

For example List.Contains Method

Dim MyVal = 4 Dim MyValues = {1,2,3,4,5,6,7}.ToList MyValues.Contains(MyVal) 

Or BinarySearch:

MyValues.Sort() Dim contains = MyValues.BinarySearch(MyVal) > -1 

Or Any

MyValues.Any(Function(item)item=MyVal) 

1 Comment

This is a very good answer. I did not know you could create and populate a list inline like this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.