按名称从数组中获取值

时间:2013-10-08 20:07:06

标签: arrays vb.net

我试图通过名称从数组中获取值。这样做的目的是将这些参数发送到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

结束班

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

2 个答案:

答案 0 :(得分:2)

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

Dim p As ParameterValue = parameters.FirstOrDefault(function(x) x.Name = "StartDate")

答案 1 :(得分:1)

你的第二次尝试越来越近了。这样的事情会起作用:

Dim startdate As DateTime = DateTime.Parse(parametersTransactional(Array.FindIndex(parametersTransactional, Function(x) x.Name = "StartDate")).Value)