如何在 vb.net 中的字典中迭代?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18628917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:08:09  来源:igfitidea点击:

How can iterate in Dictionary in vb.net?

vb.netdictionary

提问by Andy

I create a dictionary

我创建了一个字典

    Dim ImageCollection As New Dictionary(Of ConvensionImages, Integer)

and I fill that

我填补了那个

 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

now I want to search ImageID and key how can I do this

现在我想搜索 ImageID 和关键我该怎么做

回答by Andrey Gordeev

Make Integeras key for your Dictionary:

设为Integer字典的键:

Dim ImageCollection As New Dictionary(Of Integer, ConvensionImages)

Change ImageCollection.Add(obj, key)to ImageCollection.Add(key, obj)

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

And use this loop:

并使用这个循环:

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  

回答by SysDragon

You can loop this way, too:

你也可以这样循环:

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

It is very fast and simple way to it.

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