Reputation: 10542
Hey all i have about 4 dicionarys holding values for variuos things. Each dictionary has the same key for each item within each of the 4 dictionary.
Example:
dictionary1 (string, string):
key = 523697777, value = "bobs burgers"
key = 89557, value = "Blah blah 1"
key = 598823644, value = "something"
dictionary2 (string, string):
key = 523697777, value = "oats and honey"
key = 89557, value = "juicyfruit"
key = 598823644, value = "sun glasses"
dictionary3 (string, datetime):
key = 523697777, value = 01/05/2013 00:00:00
key = 89557, value = 01/24/2013 00:00:00
key = 598823644, value = 03/12/2013 00:00:00
dictionary4 (string, string):
key = 523697777, value = "Computers"
key = 89557, value = "IM"
key = 598823644, value = "cans"
Now i want to be able to just loop and get the correct value from each dicionary without having to loop through each dicionary seprate.
Currenly i am doing this:
Dim allTogether As New StringBuilder
For Each dict1 In dictionary1
For Each dict2 In dictionary2
If dict1.Key = dict2.Key Then
allTogether.Append(dict1.Value)
allTogether.Append(dict2.Value)
For Each dict2 In dictionary3
If dict2.Key = dict3.Key Then
allTogether.Append(dict3.Value)
For Each dict2 In dictionary3
If dict3.Key = dict4.Key Then
allTogether.Append(dict4.Value)
End If
Next
End If
Next
End If
Next
Next
Should produce:
bobs burgers oats and honey 01/05/2013 00:00:00 Computers
Blah blah 1 juicyfruit 01/24/2013 00:00:00 IM
something sun glasses 03/12/2013 00:00:00 cans
Is it possible to get the data in one swoop?
Upvotes: 0
Views: 202
Reputation: 31393
Also, if the keys definitely exist in all four dictionaries all of the time then as a suggestion, why not :
Public Structure myStruct
Public thing1 As String
Public thing2 As String
Public dateThing As DateTime
Public thing3 As String
End Structure
Private myDictionary As Dictionary(Of String, myStruct)
if you are not guaranteed to have the same keys in all dictionaries, why not just use the features of the dictionary?
If dictionary1.ContainsKey(someKey) Then allTogether.Append(dictionary1(someKey))
etc.
Upvotes: 0
Reputation: 39767
This should work - if you still need to use stringbuilder.
For Each sKey In Dictionary1.Keys
allTogether.Append(Dictionary1(sKey))
allTogether.Append(Dictionary2(sKey))
allTogether.Append(Dictionary3(sKey))
allTogether.Append(Dictionary4(sKey))
allTogether.Append("---------------")
Next
I've added last "--------------" as separator between sets.
Upvotes: 0
Reputation: 545518
Get the keys:
Dim keys = dictionary1.Keys
Iterate over them:
For Each key In Keys
Construct result (here: print to console):
Console.WriteLine("{0} {1} {2} {3}",
dictionary1(key),
dictionary2(key),
dictionary3(key),
dictionary4(key))
End loop.
Next
Upvotes: 3