0

我正在尝试按名称从数组中获取一个值。这样做的目的是将这些参数发送到 SSRS 和事务查询。

我有一个 XML 类型的 ParameterValue:

'report paramerters
Dim parametersTransactional(3) As ParameterValue

parametersTransactional(0) = New ParameterValue()
parametersTransactional(0).Name = "StartDate"
parametersTransactional(0).Value = "2012-01-01T00:00:00"

parametersTransactional(1) = New ParameterValue()
parametersTransactional(1).Name = "EndDate"
parametersTransactional(1).Value = "2012-06-01T00:00:00"

parametersTransactional(2) = New ParameterValue()
parametersTransactional(2).Name = CustomersCustomerLocation"
parametersTransactional(2).Value = "1, 2, 3, 4, 5"

我想我可以得到一个名字的索引然后得到这样的值;

Dim startDate As String = "StartDate"
Dim idxStartDate As Integer = Array.IndexOf(parameters, startDate)

或者像这样:

Dim start2 As Integer = parameters.FindIndex(startDate)

ParameterValue 的类型为:

'''<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.17929"),  _
 System.SerializableAttribute(),  _
 System.Diagnostics.DebuggerStepThroughAttribute(),  _
 System.ComponentModel.DesignerCategoryAttribute("code"),  _
 System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://schemas.microsoft.com/sqlserver/2005/06/30/reporting/reportingservices")>  _
Partial Public Class ParameterValue
Inherits ParameterValueOrFieldReference

Private nameField As String

Private valueField As String

Private labelField As String

'''<remarks/>
Public Property Name() As String
    Get
        Return Me.nameField
    End Get
    Set
        Me.nameField = value
    End Set
End Property

'''<remarks/>
Public Property Value() As String
    Get
        Return Me.valueField
    End Get
    Set
        Me.valueField = value
    End Set
End Property

'''<remarks/>
Public Property Label() As String
    Get
        Return Me.labelField
    End Get
    Set
        Me.labelField = value
    End Set
End Property

结束类

所以我错过了一些关键的步骤。

4

2 回答 2

2

您可以使用FirstorFirstOrDefault方法轻松完成此操作。它们都是 LINQ 扩展方法。例如:

Dim p As ParameterValue = parameters.FirstOrDefault(function(x) x.Name = "StartDate")
于 2013-10-08T20:14:50.707 回答
1

你的第二次尝试接近了。像这样的东西会起作用:

Dim startdate As DateTime = DateTime.Parse(parametersTransactional(Array.FindIndex(parametersTransactional, Function(x) x.Name = "StartDate")).Value)
于 2013-10-08T20:21:16.170 回答