【问题标题】:Equivalent of SQL IN in VB.NETVB.NET 中的 SQL IN 等价物
【发布时间】:2011-07-14 15:45:06
【问题描述】:

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

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,则它不起作用。为什么会这样,我能做些什么来解决这个问题?谢谢。

【问题讨论】:

    标签: asp.net vb.net comparison-operators


    【解决方案1】:

    玩了一会儿,我找到了一个不错的解决方案:

    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
    

    【讨论】:

      【解决方案2】:
      Dim Numbers() As Integer = {1, 2}
      If Numbers.Any(Function(i) i = Number) Then
      

      【讨论】:

      • 谢谢 - 我也试过这个,但我收到错误“'Any' is not a member of 'System.Array'”。
      • 您至少需要 .NET Framework 3.5 和对 Linq 库的引用。 Joel,为什么不使用 Contains?
      【解决方案3】:

      您正在创建 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)
      

      【讨论】:

      • 感谢您的帮助 - 但是我使用此方法收到错误“'Contains' 不是 'System.Array' 的成员”。
      • 您使用的是哪个版本的 .Net? Contains 是 3.5 版本中可用的扩展方法。我已经为之前的版本添加了一个编辑。
      • 这很奇怪,因为我正在开发的网站是 3.5 版!
      • 奇怪 - Contains 在智能感知中显示吗?我认为它没有。
      • 不,它不适用于 Array 对象,但它适用于 ArrayLists。
      猜你喜欢
      • 1970-01-01
      • 2018-11-27
      • 2016-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-27
      • 2020-08-04
      相关资源
      最近更新 更多