.net 如何使用嵌套字典中的已知键访问值
我编了一本字典.net 如何使用嵌套字典中的已知键访问值,.net,vb.net,dictionary,.net,Vb.net,Dictionary,我编了一本字典 Dim north_dict As New Dictionary(Of String, Dictionary(Of String, String)) 每个循环I的内部 Dim dict_north As New Dictionary(Of String, String) 为north_dict中的每个键创建嵌套字典 每个嵌套字典都有相同的35个键 示例键y7x1、y7x2 在不遍历字典的情况下,如何从嵌套字典中检索已知键的值 Dim north_files() As Stri
Dim north_dict As New Dictionary(Of String, Dictionary(Of String, String))
每个循环I的内部
Dim dict_north As New Dictionary(Of String, String)
为north_dict中的每个键创建嵌套字典
每个嵌套字典都有相同的35个键
示例键y7x1、y7x2
在不遍历字典的情况下,如何从嵌套字典中检索已知键的值
Dim north_files() As String = Directory.GetFiles(txtbx_north_location.Text)
For Each file As String In north_files
Dim dict_north As New Dictionary(Of String, String)
Dim reader_for_files As New StreamReader(file)
Dim allLines As List(Of String) = New List(Of String)
Do While Not reader_for_files.EndOfStream
allLines.Add(reader_for_files.ReadLine())
Loop
reader_for_files.Close()
y7x1 = ReadLine(2, allLines)
dict_north.Add("y7x1", y7x1)
y7x2 = ReadLine(4, allLines)
dict_north.Add("y7x2", y7x2)
Dim result As String
result = Path.GetFileName(file)
north_dict.Add(result, dict_north)
下一个
以致
Dim sbuild As New StringBuilder
For Each item As KeyValuePair(Of String, Dictionary(Of String, String)) In north_dict
sbuild.AppendLine(item.Key & ") " & item.Value.ToString)
Next
MessageBox.Show(sbuild.ToString)
产生如下结果
NORTH_COL_10ROW_1)System.Collections.Generic.Dictionary'2[System.String,System.String]
NORTH_COL_10ROW_10)System.Collections.Generic.Dictionary'2[System.String,System.String]
我希望能够从NORTH_COL_4ROW_2获取例如键y7x1的值
我还不知道嵌套字典是否被成功添加。是否考虑过使用linq?会是这样的。这是一个有效的例子,仍然不确定我是否正确理解了你的问题
For n = 1 To 100
Dim dict_north As New Dictionary(Of String, String)
For m = 1 To 35
dict_north.Add("y7x" & m.ToString(), "Value for y7x " & m.ToString())
Next
north_dict.Add(n.ToString(), dict_north)
Next
Dim s = (From nd In north_dict
From dn In nd.Value
Where dn.Key = "y7x2"
Select nd.Key & " - " & dn.Value).ToList()
Debug.Print(String.Join(" ,", s.ToArray()))
结果是
1-Y7x2的值,2-Y7x2的值,3-Y7x2的值…
Dim dictvalue As String = ""
If north_dict("NORTH_COL_4ROW_2").TryGetValue("y7x1", dictvalue) Then
MessageBox.Show(dictvalue.ToString)
End If
类似于
sbuild.AppendLine(item.Key&“)和item.Value(“y7x1”)
?谢谢你,马克,我刚刚在学习。这表明我的字典正在工作。现在我必须弄清楚如何在循环之外单独访问它们。