简体   繁体   English

等效于 VB.NET 中的 SQL IN

[英]Equivalent of SQL IN in VB.NET

What I am trying to do is to check if a value matches one of two numbers (and easily be able to add to the numbers to compare to).我要做的是检查一个值是否与两个数字之一匹配(并且很容易添加到要比较的数字中)。 Rather than doing a longer-winded way such as:而不是做一个冗长的方式,例如:

If Number = 1 Or Number = 2 Then ...

I'm trying to do something like this:我正在尝试做这样的事情:

If Number In (1,2) Then...

As the In operator isn't available in VB, I have tried the following code instead:由于In运算符在 VB 中不可用,因此我尝试了以下代码:

Protected SectionID As Integer = HttpContext.Current.Request.QueryString("sectionid")
Protected PageID As Integer = HttpContext.Current.Request.QueryString("pageid")

Protected Sub HotspotsLV_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles HotspotsLV.ItemDataBound
    Dim SecondLineHolder As HtmlControl = e.Item.FindControl("SecondLineHolder")
    Select Case True
        Case New String("2", "3").Contains(SectionID) : SecondLineHolder.Attributes("style") = "color:#21720B"
        Case New String("8", "12").Contains(PageID) : SecondLineHolder.Attributes("style") = "color:#1B45C2"
    End Select
End Sub

I have found that this only works when SectionID is 2 or PageID is 8. If SectionID is 3 or PageID is 12 then it doesn't work.我发现这仅在SectionID为 2 或PageID为 8 时有效。如果SectionID为 3 或PageID为 12 则它不起作用。 Why is this and what can I do to try to get around the problem?为什么会这样,我能做些什么来解决这个问题? Thanks.谢谢。

After a bit of playing around, I have managed to find a nice solution:经过一番玩耍,我设法找到了一个不错的解决方案:

Select Case True
    Case Array.IndexOf(New Integer() {2, 3}, SectionID) > -1 : SecondLineHolder.Attributes("style") = "color:#21720B"
    Case Array.IndexOf(New Integer() {8, 12}, PageID) > -1 : SecondLineHolder.Attributes("style") = "color:#1B45C2"
End Select
Dim Numbers() As Integer = {1, 2}
If Numbers.Any(Function(i) i = Number) Then

You are creating a String instance not an array.您正在创建一个String实例而不是一个数组。 Try changing it to:尝试将其更改为:

Protected SectionID As Integer = HttpContext.Current.Request.QueryString("sectionid")
Protected PageID As Integer = HttpContext.Current.Request.QueryString("pageid")

Protected Sub HotspotsLV_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles HotspotsLV.ItemDataBound
    Dim SecondLineHolder As HtmlControl = e.Item.FindControl("SecondLineHolder")
    Dim sections As Integer() = New Integer(){2,3}
    Dim pages As Integer() = New Integer(){8,12}
    Select Case True
        Case sections.Contains(SectionID) : SecondLineHolder.Attributes("style") = "color:#21720B"
        Case pages.Contains(PageID) : SecondLineHolder.Attributes("style") = "color:#1B45C2"
    End Select
End Sub

If you use Option Strict On then the type mismatch would be highlighted.如果您使用Option Strict On ,则将突出显示类型不匹配。 In your initial code New String("2", "3") would create a string with a value of 222 .在您的初始代码New String("2", "3")将创建一个值为222的字符串。

Edit编辑

For.Net version prior to 3.5 the Contains method will not be available. For.Net 3.5 之前的版本将无法使用Contains方法。 This can be mimicked using IndexOf :这可以使用IndexOf来模仿:

Array.IndexOf(sections, SectionID) > -1
' Equivalent to sections.Contains(SectionID)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM