2013-08-30 120 views
0

嘿,我有大约4个dicionarys持有variuos事物的值。每个字典对于每个字典中的每个项目都有相同的密钥。VB.net在所有字典中找到相同的密钥

例子:

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" 

现在我想能够只是循环,并从每个dicionary正确的值,而无需通过每个dicionary seprate不必循环。

Currenly我这样做:

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 

应该产生:

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 

是否有可能得到一举中的数据?

+0

所以基本上给了一个键,你想从同一个键的所有4个字典中获取值? –

+0

@YuriyGalanter正确。 – StealthRT

回答

3

拿到钥匙:

Dim keys = dictionary1.Keys 

在它们之间迭代:

For Each key In Keys 

建构的结果(在这里:打印到控制台):

Console.WriteLine("{0} {1} {2} {3}", 
         dictionary1(key), 
         dictionary2(key), 
         dictionary3(key), 
         dictionary4(key)) 

结束循环。

Next 
+0

因为有些是String而其他人是dateTime,所以无法工作? – StealthRT

+0

@StealthRT无所谓,仍然有效。重要的是他们*键*是相同的(类型和实际值)。 –

+0

关于“key”的错误**类型'System.Collections.Generic.KeyValuePair(Of String,Date)'的值无法转换为'String'。** – StealthRT

0

另外,如果钥匙肯定是在所有4个词典存在所有的时间,然后作为一个建议,为什么不:

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 dictionary1.ContainsKey(someKey) Then allTogether.Append(dictionary1(someKey)) 

+0

在某一点上,一个二进制文件可能与其他所有文件不一样。 – StealthRT

+0

@StealthRT然后注意,如果是这种情况,其他答案将会抛出一个'KeyNotFoundException'。如上所述使用'.ContainsKey',您可以在尝试为不存在的键提取值之前进行检查。 –

+0

@StealthRT使用此选项,只需要将字符串或日期设置为空(如果不需要)。 –

0

这应该工作 - 如果你仍然需要使用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 

我添加了最后的 “--------------” 为集之间的分隔符。

相关问题