Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 使用字典的多维数组_Vb.net - Fatal编程技术网

Vb.net 使用字典的多维数组

Vb.net 使用字典的多维数组,vb.net,Vb.net,我正在尝试创建多维关联数组。我想要它,这样我就可以有: someVar(日期)(小时)(类别)=mssql查询 我正在使用以下方法进行尝试和准备,但在向阵列添加数据时遇到问题 Dim test As New Dictionary(Of Integer, Dictionary(Of String, String)) Dim test2 As New Dictionary(Of String, String) 非常感谢您的帮助 -----编辑: 这是我正在使用的,它可以根据需要工作。艾奥尼知道为什

我正在尝试创建多维关联数组。我想要它,这样我就可以有:

someVar(日期)(小时)(类别)=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
Dim测试作为新字典(整数字典,字符串字典,字符串字典))
Dim SomeNum作为整数=0
Dim someStr As String=“这是一个字符串:”
私有子按钮1\u单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理按钮1。单击
而SomeNum<100
Dim someNum2为整数=0
Dim test2作为新字典(字符串、字符串)
而有些人则<100
test2.Add(CType(someNum2,String),someStr&CType(someNum2,String))
someNum2+=1
结束时
添加(SomeNum,test2)
SomeNum+=1
结束时
对于测试中作为KeyValuePair(整数、字典(字符串、字符串))的每个kvp
Dim ccc As String=“”
Dim ddd As String=“”
尺寸v1为字符串=CType(kvp.Key,字符串)
Dim v2作为字典(字符串,字符串)=kvp.值
lblOne.Items.Add(“键:”&v1)
对于v2中的每个kvp2作为KeyValuePair(字符串的,字符串的)
尺寸v3为字符串=kvp2.Key
尺寸v4为字符串=kvp2.Value
lblTwo.Items.Add(“子键:&v3&”值:&v4)
lblOne.Items.Add(“”)
下一个
lblOne.Items.Add(v1&“End-----------------”)
lblTwo.Items.Add(v1&“End-----------------”)
下一个
端接头

查看
实体框架
它从数据库中生成对象


对于自定义数组,您可能会发现一个有用的方法。

创建一个属性为“Date”、“HourlySales”、“Category”的类

创建类型为
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

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


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

再举一个示例,说明您如何在代码中看到这一点?我需要能够存储一个键,例如一个日期,在该日期内,我需要获得每个类别的每小时销售额。(我不直接担心查询部分,一旦我让这部分工作起来,我就会计算出查询。)我已经编辑了这个问题,以显示我的工作内容。谢谢,这是很好的了解,以备将来使用,但这个应用程序已经创建了,没有它,我只是做一个补充。我知道这可能是一场噩梦,但我正在创造它的飞行,通过循环和前进。我真的不需要查任何东西。@DonnieArcher:你能看到我建议的方法的缺点吗?这不仅减少了噩梦,也减少了代码,即使你只做了一次。
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