VB.net 函数中是否有任何 IN 运算符,如 SQL 中的运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13513586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 16:44:18  来源:igfitidea点击:

Is there any IN Operator in VB.net functions like the one in SQL

.netvb.netoperators

提问by Alaa

Is there any function or operator like:

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

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

Instead of:

代替:

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

回答by LarsTech

Try using an array and then you can use the Contains extension:

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

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

Or without the declaration line:

或者没有声明行:

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

From the OP, if the Contains extension is not available, you can try this:

从 OP 中,如果包含扩展名不可用,您可以尝试以下操作:

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

回答by Meta-Knight

You can use Contains as shown by LarsTech, but it's also very easy to add an Inextension method:

您可以使用 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

You can use it like this:

你可以这样使用它:

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

回答by Botz3000

You could also use a Select..Casestatement:

您还可以使用以下Select..Case语句:

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