当前位置:  开发笔记 > 编程语言 > 正文

如何在vb.net中的Dictionary中进行迭代?

如何解决《如何在vb.net中的Dictionary中进行迭代?》经验,为你挑选了2个好方法。

我创建了一本字典

    Dim ImageCollection As New Dictionary(Of ConvensionImages, Integer)

我填写

 For Each dr As DataRow In dt.Rows
            Dim obj As New ConvensionImages
            obj.ImageID = dr("ID")
            obj.Name = dr("Name")
            obj.Description = dr("Description")
            obj.CategoryID = dr("CategoryID")
            obj.CategoryName = dr("CategoryName")
            obj.CategoryDescription = dr("CatDescription")
            obj.EventID = dr("EventID")
            obj.Image = dr("img")
            obj.DownloadImage = dr("DownLoadImg")
            ImageCollection.Add(obj, key)
            key = key + 1

现在我想搜索ImageID和密钥我该怎么做呢



1> Andrey Gorde..:

Integer为您的字典键:

Dim ImageCollection As New Dictionary(Of Integer, ConvensionImages)

更改ImageCollection.Add(obj, key)ImageCollection.Add(key, obj)

并使用此循环:

For Each kvp As KeyValuePair(Of Integer, ConvensionImages) In ImageCollection
     Dim v1 As Integer = kvp.Key  
     Dim v2 As ConvensionImages = kvp.Value  
     'Do whatever you want with v2:
     'If v2.ImageID = .... Then
Next  



2> SysDragon..:

你也可以这样循环:

For Each iKey As Integer In ImageCollection.Keys
    Dim value As ConvensionImages = ImageCollection(iKey)
    '...
Next

这是非常快速和简单的方法.

推荐阅读
N个小灰流_701
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有