VB.net中的任何IN运算符是否与SQL中的运算符类似

时间:2012-11-22 13:19:05

标签: .net vb.net operators

是否有任何函数或运算符:

        If RoleName in ( "Val1",  "Val2" ,"Val2" ) Then
        'Go
    End If

而不是:

    If RoleName = "Val1" Or RoleName = "Val2" Or RoleName = "Val2" Then
        'Go
    End If

3 个答案:

答案 0 :(得分:18)

尝试使用数组,然后您可以使用Contains扩展名:

Dim s() As String = {"Val1", "Val2", "Val3"}
If s.Contains(RoleName) Then
  'Go      
End If

或没有申报行:

If New String() {"Val1", "Val2", "Val3"}.Contains(RoleName) Then
  'Go
End If

从OP中,如果Contains扩展名不可用,您可以尝试:

If Array.IndexOf(New String() {"Val1", "Val2", "Val3"}, RoleName) > -1 Then
  'Go
End If

答案 1 :(得分:14)

你可以像LarsTech所示使用Contains,但添加In扩展方法也很容易:

Public Module Extensions

    <Extension()> _
    Public Function [In](Of T)(value As T, ParamArray collectionValues As T()) As Boolean
        Return collectionValues.Contains(value)
    End Function

End Module

你可以像这样使用它:

If RoleName.In("Val1", "Val2", "Val3") Then
    'Go
End If

答案 2 :(得分:11)

您还可以使用Select..Case声明:

Select Case RoleName 
    Case "Val1", "Val2", "Val3"
        ' Whatever
End Select