我有两本字典;
我想从字典B中筛选(或得到另一本过滤字典)字典A。我的意思是,二分法B包含字典A键的一些键/值,我想要一个只有这些键的A字典。
以防万一,B不一定是(int64,int64)的字典,它可以是一个列表、一个数组或其他任何东西,如果它有帮助的话。
非常感谢!
PS:我知道我可以用一个for里面的一个for,但我想(希望?)会有更有效的方法来做到这一点。
发布于 2011-05-11 22:03:32
这有帮助吗?Customer有一个属性ID类型为int64。
Dim foo As New Dictionary(Of Int64, Int64)
Dim bar As New Dictionary(Of Int64, Customer)
foo.Add(1, 5)
foo.Add(2, 99)
foo.Add(3, 222)
foo.Add(4, 333)
bar.Add(1, New Customer(5, "john"))
bar.Add(55, New Customer(323, "ringo"))
bar.Add(4, New Customer(333, "george"))
Dim common = From f In foo, b In bar _
Where f.Key = b.Key _
And f.Value = b.Value.ID _
Select b
For Each item As KeyValuePair(Of Int64, Customer) In common
Console.WriteLine(item.Key & " " & item.Value.ID & " " & item.Value.Name)
Next
....
Public Class Customer
Public ID As Int64
Public Name As String
End Classhttps://stackoverflow.com/questions/5970986
复制相似问题