我有两本词典:
我想知道是否存在包含" abcd"的密钥。我不能硬编码" abcd"。下面的代码提供了一个输出" abcd.0001在新文件"中删除,这是真的,但我希望代码只查找密钥的一部分,如果它在两个文件中都有,它不应该写输出。
谢谢!
For Each kvp As KeyValuePair(Of String, Integer) In file1dictionary
If file2dictionary.ContainsKey(kvp.Key) Then
Else
objStreamWriter.WriteLine(kvp.Value.ToString() + " entries of " + kvp.Key + " DELETED in new PDI")
End If
Next
答案 0 :(得分:0)
<强> EDITED 强>
这应该是你想要的:
Private Sub Test()
Dim file1dictionary As New Dictionary(Of String, Integer), file2dictionary As New Dictionary(Of String, Integer)
file1dictionary("abcd.0001") = 1
file2dictionary("abcd.0002") = 1
file2dictionary("defg.0001") = 1
Dim correspondences As Integer
For Each kvp1 As KeyValuePair(Of String, Integer) In file1dictionary
correspondences = file2dictionary.ToArray.Count(
Function(kvp2) kvp2.Key.StartsWith(kvp1.Key.Substring(0, kvp1.Key.IndexOf("."c))))
If correspondences > 0 Then
'nothing done in this case
Else
MsgBox(kvp1.Value.ToString() + " entries of " + kvp1.Key + " DELETED in new PDI")
End If
Next
End Sub
但我会用这种方式编写代码,简而言之:
For Each kvp In file1dictionary.Where(
Function(kvp1) file2dictionary.ToArray.Any(
Function(kvp2) kvp2.Key.StartsWith(kvp1.Key.Substring(0, kvp1.Key.IndexOf("."c)))))
objStreamWriter.WriteLine(kvp.Value.ToString() + " entries Of " + kvp.Key + " DELETED In New PDI")
Next