相当于 VB.NET 中的 SQL IN

发布于 2024-11-19 20:08:35 字数 1106 浏览 7 评论 0原文

我想做的是检查一个值是否与两个数字之一匹配(并且可以轻松地添加到要比较的数字中)。 我没有采用冗长的方式,例如:

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

我正在尝试这样做:

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

由于 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

我发现仅当 SectionID 为 2 或 PageID 为 8 时才有效。如果 SectionID 为 3 或 PageID 为 12,则它不起作用。这是为什么?我可以采取什么措施来解决这个问题?谢谢。

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:

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. Why is this and what can I do to try to get around the problem? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

段念尘 2024-11-26 20:08:36

经过一番尝试后,我找到了一个很好的解决方案:

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

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
白云悠悠 2024-11-26 20:08:36
Dim Numbers() As Integer = {1, 2}
If Numbers.Any(Function(i) i = Number) Then
Dim Numbers() As Integer = {1, 2}
If Numbers.Any(Function(i) i = Number) Then
谎言月老 2024-11-26 20:08:36

您正在创建一个 String 实例而不是数组。尝试将其更改为:

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

如果您使用Option Strict On,则类型不匹配将突出显示。在初始代码中,New String("2", "3") 将创建一个值为 222 的字符串。

编辑

对于 3.5 之前的 .Net 版本,Contains 方法将不可用。这可以使用 IndexOf 来模仿:

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

You are creating a String instance not an array. 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. In your initial code New String("2", "3") would create a string with a value of 222.

Edit

For .Net version prior to 3.5 the Contains method will not be available. This can be mimicked using IndexOf:

Array.IndexOf(sections, SectionID) > -1
' Equivalent to sections.Contains(SectionID)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文