【发布时间】:2012-10-08 14:34:21
【问题描述】:
我正在使用以下代码:
Dim questions = db.Tbl_Challenge_Questions.Where(Function(x) x.Quest_Challenge_ID = challengeId)
Dim answersToQuestions = New Collection
For Each question As Tbl_Challenge_Question In questions
Dim questionId = question.Quest_ID
Dim answer = question.Tbl_Challenge_Question_Answer.Where(Function(x) x.QAns_Question_ID = questionId).FirstOrDefault.QAns_Answer()
Debug.Print("Quest_ID=" + question.Quest_ID.ToString)
Debug.Print("answer=" + answer)
'answersToQuestions.Add(question.Quest_ID, answer)
Next
哪个输出这个:
Quest_ID=1
answer=True
Quest_ID=2
answer=150 minutes
Quest_ID=3
answer=True
Quest_ID=4
answer=False
Quest_ID=5
answer=Continuing to smoke
当我取消注释要添加到集合 answersToQuestions.Add(question.Quest_ID, answer) 的行时,它会输出以下错误:
Quest_ID=1
answer=True
Quest_ID=2
answer=150 minutes
Quest_ID=3
answer=True
A first chance exception of type 'System.ArgumentException' occurred in Microsoft.VisualBasic.dll
The program '[4948] WebDev.WebServer40.EXE: Managed (v4.0.30319)' has exited with code 0 (0x0).
添加失败。提供了重复的键值。
似乎没有任何重复。如何将所有这些项目添加到我的收藏中?
谢谢。
【问题讨论】:
-
使用强类型集合不是更好吗,比如 Dictionary(of Integer, String)?
-
Collection是什么类型请提供代码或 MSDN 链接 -
@Jodrell - 我怀疑它是 this one,因为它是我能找到的唯一一个 a) 称为
Collection,b) 不是通用的,c) 有一个Add方法接受多个参数。 -
@Damien_The_Unbeliever,是的,很明显。在不经常使用它之后,人们忘记了
Microsoft.VisualBasic命名空间的好奇心。
标签: asp.net-mvc vb.net collections