从字典(int64,int64)过滤字典(int64,myObject)

时间:2011-05-11 21:43:44

标签: .net vb.net dictionary filter

我有2本词典;

  • A:字典(int64,myObject)
  • B:字典(int64,int64)。

我想从字典B中过滤(或获取另一个过滤字典)字典A.我的意思是,dictonary B包含字典A键的一些键/值,我只想要一个带有这些键的字典。

以防万一,B不必是(int64,int64)的字典,它可以是一个列表,一个数组或任何其他东西,如果它有帮助。

非常感谢!

PS:我知道我可以用for for a for a做,但我想(希望?)会有更有效的方法来做。

1 个答案:

答案 0 :(得分:1)

这有帮助吗? 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 Class