Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 无法添加到VB.NET中的集合(重复异常)_Asp.net Mvc_Vb.net_Collections - Fatal编程技术网

Asp.net mvc 无法添加到VB.NET中的集合(重复异常)

Asp.net mvc 无法添加到VB.NET中的集合(重复异常),asp.net-mvc,vb.net,collections,Asp.net Mvc,Vb.net,Collections,我正在使用以下代码: 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 = quest

我正在使用以下代码:

    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.Addquestion.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).
添加失败。提供了重复的键值

似乎没有任何重复。如何将所有这些项目添加到我的收藏中


谢谢。

您可以试试这个,看看它是否有效:

Dim questions = db.Tbl_Challenge_Questions.Where(Function(x) x.Quest_Challenge_ID = challengeId)

    Dim answersToQuestions = New Dictionary(Of Integer, String)

    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







    Next

你能试试这个吗

Dim questions = db.Tbl_Challenge_Questions.Where(Function(x) x.Quest_Challenge_ID = challengeId)

    Dim answersToQuestions = New Dictionary(Of Integer, String)

    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







    Next

实际上,您使用的是用于遗留代码库的VisualBasic特定类

其方法希望先传递项,然后传递键,而不是先传递键,然后传递项:

您已经在其中有一个值,键为True


正如@Paul Grimshaw所建议的,我还建议使用一个泛型类型,例如。

实际上,您使用的是用于遗留代码库的特定于visualbasic的类

其方法希望先传递项,然后传递键,而不是先传递键,然后传递项:

您已经在其中有一个值,键为True


正如@Paul Grimshaw所建议的,我还建议使用一个泛型类型,例如。

使用一个强类型的集合,例如整数、字符串的字典,不是更好吗?集合是什么类型请提供代码或链接MSDN@Jodrell-我想是因为这是我能找到的唯一一个叫收藏的,b不是泛型的,c有一个接受多个参数的Add方法。@Damien_不信者,是的,很明显。在不经常使用它之后,人们会忘记Microsoft.VisualBasic命名空间的奇特之处。使用强类型集合(例如整数字典)不是更好吗,字符串?集合是什么类型?请提供代码或链接MSDN@Jodrell-我怀疑这是因为它是我能找到的唯一一个被调用的集合,b不是泛型的,c有一个接受多个参数的Add方法。@Damien_不相信,是的,很明显。在不经常使用它之后,人们会忘记Microsoft.VisualBasic名称空间的奇特之处。这非常有效。谢谢我仍然习惯于VB.NET中不同类型的集合。在我的原生PHP中,只有一个:array:@user1477388,我通常避免使用Microsoft.VisualBasic名称空间。对我来说,很难掌握哪些函数属于哪个名称空间。这非常有效。谢谢我仍然习惯于VB.NET中不同类型的集合。在我的原生PHP中,只有一个:array:@user1477388,我通常避免使用Microsoft.VisualBasic名称空间。对我来说,很难掌握哪些函数属于哪个名称空间。