Asp.net VB.NET中SQL的等价性
我试图做的是检查一个值是否与两个数字中的一个匹配(并且能够轻松地添加到要比较的数字中)。而不是以冗长的方式,例如:Asp.net VB.NET中SQL的等价性,asp.net,vb.net,comparison-operators,Asp.net,Vb.net,Comparison Operators,我试图做的是检查一个值是否与两个数字中的一个匹配(并且能够轻松地添加到要比较的数字中)。而不是以冗长的方式,例如: If Number = 1 Or Number = 2 Then ... 我正在尝试这样做: If Number In (1,2) Then... 由于中的操作符在VB中不可用,因此我尝试了以下代码: Protected SectionID As Integer = HttpContext.Current.Request.QueryString("sectionid") Pro
If Number = 1 Or Number = 2 Then ...
我正在尝试这样做:
If Number In (1,2) Then...
由于中的操作符在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,则它不起作用。为什么会这样?我能做些什么来解决这个问题?谢谢。您正在创建一个字符串
实例,而不是数组。尝试将其更改为:
Dim Numbers() As Integer = {1, 2}
If Numbers.Any(Function(i) i = Number) Then
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
如果在
上使用选项,则类型不匹配将突出显示。在初始代码中,新字符串(“2”、“3”)
将创建一个值为222的字符串
编辑
对于3.5之前的.Net版本,Contains
方法将不可用。这可以使用IndexOf
进行模拟:
Array.IndexOf(sections, SectionID) > -1
' Equivalent to sections.Contains(SectionID)
经过一番尝试,我终于找到了一个不错的解决方案:
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
谢谢你的帮助-但是我得到错误“'Contains'不是'System.Array'的成员”使用此方法。你使用的是哪个版本的.Net<代码>包含
是一种可从3.5版获得的扩展方法。我已经包括了一个编辑版本在此之前。这是奇怪的,因为我的网站是在3.5版本的工作!Wierd-是否包含以智能感知方式显示?我假设它不是。不,它不适用于数组对象,尽管它适用于ArrayList。谢谢-我也尝试过这一点,但我得到错误“'Any'不是'System.Array'的成员”。您至少需要.NET Framework 3.5和对Linq库的引用。乔尔,为什么不使用它呢?