首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从字典( int64,myObject)中过滤( int64,int64)

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

Stack Overflow用户
提问于 2011-05-11 21:43:44
回答 1查看 526关注 0票数 1

我有两本字典;

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

我想从字典B中筛选(或得到另一本过滤字典)字典A。我的意思是,二分法B包含字典A键的一些键/值,我想要一个只有这些键的A字典。

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

非常感谢!

PS:我知道我可以用一个for里面的一个for,但我想(希望?)会有更有效的方法来做到这一点。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-05-11 22:03:32

这有帮助吗?Customer有一个属性ID类型为int64

代码语言:javascript
复制
    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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5970986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档