0

我正在尝试制作一个多维关联数组。我想要它,这样我就可以拥有类似的东西:

someVar(date)(hour)(category) = mssql 查询

我正在使用以下内容来尝试准备,但无法将数据添加到数组中。

Dim test As New Dictionary(Of Integer, Dictionary(Of String, String))
Dim test2 As New Dictionary(Of String, String)

任何帮助是极大的赞赏。

-----编辑:这是我正在使用的,它可以按需要工作。有人知道为什么这是一个不好的方法吗?

Dim test As New Dictionary(Of Integer, Dictionary(Of String, String))
Dim SomeNum As Integer = 0
Dim someStr As String = "This is a string: "


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    While SomeNum < 100
        Dim someNum2 As Integer = 0
        Dim test2 As New Dictionary(Of String, String)

        While someNum2 < 100
            test2.Add(CType(someNum2, String), someStr & CType(someNum2, String))

            someNum2 += 1
        End While
        test.Add(SomeNum, test2)
        SomeNum += 1


    End While

    For Each kvp As KeyValuePair(Of Integer, Dictionary(Of String, String)) In test
        Dim ccc As String = ""
        Dim ddd As String = ""
        Dim v1 As String = CType(kvp.Key, String)
        Dim v2 As Dictionary(Of String, String) = kvp.Value

        lblOne.Items.Add("Key: " & v1)

        For Each kvp2 As KeyValuePair(Of String, String) In v2

            Dim v3 As String = kvp2.Key
            Dim v4 As String = kvp2.Value

            lblTwo.Items.Add("SubKey: " & v3 & " Value: " & v4)

            lblOne.Items.Add("")

        Next

        lblOne.Items.Add(v1 & " End--------------")
        lblTwo.Items.Add(v1 & " End--------------")
    Next

End Sub
4

3 回答 3

1

创建一个具有“日期”、“小时销售”、“类别”属性的类。

Public Class Sales
    Public Property SalesDate() As Date
    Public Property HourlySales() As Decimal
    Public Property Category() As String

    Public Sub New()
    End Sub

    Public Sub New(vSalesDate As Date, vHourlySales As Decimal, vCategory As String)
        SalesDate = vSalesDate
        HourlySales = vHourlySales
        Category = vCategory
    End Sub
End Class

创建类型对象的列表Sales

Shared Function GetSales() As List(Of Sales)
        Dim SalesList As New List(Of Sales)

        Using connection As New SqlConnection(YourConnectionString)
            Dim cmd As SqlCommand = New SqlCommand("SelectSalesList", connection)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection.Open()

            Dim reader As SqlDataReader = cmd.ExecuteReader()

            While reader.Read
                SalesList.Add(New Sales(reader("SalesDate"), reader("HourlySales"), reader("Category")))
            End While
        End Using

        Return SalesList
    End Function

您可以调用该GetSales()函数以返回Sales.

于 2013-04-29T23:18:50.473 回答
0

字典字典是维护的噩梦,查询和调试。

我建议改为使用复合键,因此您只需对日期+小时+类别的字符串进行一次查找,而不是进行 3 次查找。例如,date=Monday,hour=9PM,category=Apples,你的键是Monday:9PM:Apples(我选择冒号作为部分分隔符,但你可以选择不同的字符)。

于 2013-04-30T01:05:26.987 回答
0

Entity Framework调查它会使对象脱离您的数据库。

MSDN 英孚

对于自定义数组,您可能会发现Tuple很有用。

于 2013-04-29T20:57:18.623 回答