“System.Collections.Hashtable 的一维数组”类型的值无法转换为“System.Collections.Hashtable”

Value of type '1-dimensional array of System.Collections.Hashtable' cannot be converted to 'System.Collections.Hashtable

我正在使用 hastable,但是当我向它添加值时出现错误

“System.Collections.Hashtable 的一维数组”类型的值无法转换为 'System.Collections.Hashtable'

我是不是很想念最基本的东西。

  Public Shared Function LstItemsAsHT() As Hashtable()

        Dim htStudents As Hashtable = New Hashtable()

        Try
            Using dbcontext As New Entities
                Dim result = From l In dbcontext.students
                             Where l.Pass = False
                              Select l

                For Each student In result.ToList()
                    htReceipt.Add(student.Id, student.Type)

                Next
            End Using

        Catch ex As Exception

        End Try

        Return htStudents 

    End Function

您的方法 returns 一个 HashTable() 而不是一个 HashTable。将其更改为:

Public Shared Function LstItemsAsHT() As Hashtable

除此之外,如果可以使用强类型 Dictionary(Of Int32, String),为什么还要使用 Hashtable?此外,不需要 result.ToList() 。您可以立即枚举序列,而无需先创建新列表。