2012-11-01 90 views
1

嘿,我需要一些帮助循环通过我的字典列表。我似乎无法找到正确的语法。VB.net字典循环

这里是我的代码:

Dim all = New Dictionary(Of String, Object)() 
Dim info = New Dictionary(Of String, Object)() 
Dim theShows As String = String.Empty 

info!Logo = channel.SelectSingleNode(".//img").Attributes("src").Value 
info!Channel = .SelectSingleNode("channel.//span[@class='channel']").ChildNodes(1).ChildNodes(0).InnerText 
info!Station = .SelectSingleNode("channel.//span[@class='channel']").ChildNodes(1).ChildNodes(2).InnerText 
info!Shows = From tag In channel.SelectNodes(".//a[@class='thickbox']") 
      Select New With {channel.Show = tag.Attributes("title").Value, channel.Link = tag.Attributes("href").Value} 

all.Add(info!Station, info.Item("Shows")) 

theShows = all.Item("Shows") '<--Doesnt work... 

我只想提取无论是在“显示” 从所有字典

enter image description here

+0

@LarsTech在代码中添加了该部分。对不起,离开了! – StealthRT

+0

'all.Add(info!Station,info.Item(“Shows”))' - 与您的风格保持一致,不要在同一个语句中更改项目访问的语法!事实上,不要使用'.Item(...)'方法,要么写'info!shows'或'info(“Shows”)''。但是,要保持一致,不要混用风格。 –

+1

当您声明字典时,对象是KEY,VALUE。在你的代码中,你的键是字符串,它会输出一个对象。当您执行“theShows = all.Item(”shows“)”时,您要求拉动与“演出”键关联的对象。这是你的意图吗?因为“显示”似乎是一个IEnumerable,并且您试图将其分配给一个String变量。 – Joe

回答

1

您的代码,

all.Add(info!Station, info.Item("Shows")) 

theShows = all.Item("Shows") 

info!Station的值被用作在all字典中的KEY值。然后尝试使用常量字符串"Shows"访问该值。我不知道你的意图是什么,但

theShows = all.Item(info!Station) 

应该返回已使用密钥存储info!StationShows值。

如果你想显示的列表作为字符串,你可以这样做,

Dim Shows as String = "" 
For Each item in theShows 
    Shows &= item.Show & vbNewLine 
Next 
+0

感谢您的代码。我得到一个**类型'WhereSelectEnumerableIterator(Of HtmlNode,VB $ AnonymousType_0(Of String,String))'转换为类型'String'的错误**无效**当我将其更改为** theShows = all时。 Item(info!Station)** – StealthRT

+0

因为'info!Shows1'最初是从您的Linq查询中分配的,它返回一个匿名类型的列表,所以我将删除'Dim theShows As String = String.Empty'行,然后将'theShows = all.Item(info!Station)'改为'Dim theShows = all.Item(info!Station)' – Kratz

+0

我试图从节目中提取数据,但是按照你的建议,这样做仍然需要我回到正方形1.检查我刚刚更新的OP中的图像。 – StealthRT

1

你也可以遍历这样

For Each pair As KeyValuePair(Of String, String) In dict 

    MsgBox(pair.Key & " - " & pair.Value) 
Next 

来源:VB.Net Dictionary

温斯顿