Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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
Vb.net 链接异常/错误_Vb.net_Linq - Fatal编程技术网

Vb.net 链接异常/错误

Vb.net 链接异常/错误,vb.net,linq,Vb.net,Linq,长期以来,我一直在尝试这样做: Private Sub Search() 'DataGrid1.ItemsSource = From k In Globals.Batchs.BatchList Where (CType(k.Category, String).ToLower().Contains(SearchByCategory.Text)) Select k Dim tot As List(Of WorkMateLib.BatchLib.BatchItem) = Globals

长期以来,我一直在尝试这样做:

Private Sub Search()
    'DataGrid1.ItemsSource = From k In Globals.Batchs.BatchList Where (CType(k.Category, String).ToLower().Contains(SearchByCategory.Text)) Select k
    Dim tot As List(Of WorkMateLib.BatchLib.BatchItem) = Globals.Batchs.BatchList
    If Not SearchByCategory.Text = "" Then
        tot = From k As WorkMateLib.BatchLib.BatchItem In tot Where CType(k.Category, String).ToLower().Contains(SearchByCategory.Text) Select k
    End If
    If Not SearchByCourse.Text = "" Then
        tot = From k In tot Where (CType(k.CourseName, String).ToLower().Contains(SearchByCourse.Text))
    End If
    If Not SearchByName.Text = "" Then
        tot = From k In tot Where (CType(k.BatchName, String).ToLower().Contains(SearchByName.Text))
    End If
    If Not SearchByYear.Text = "" Then
        tot = From k In tot Where (CType(k.DateStarted, Date).Year.ToString.ToLower.Contains(SearchByYear.Text))
    End If
    DataGrid1.ItemsSource = tot
End Sub
但它会继续抛出以下异常:

Unable to cast object of type 'WhereSelectListIterator`2[WorkMateLib.BatchLib.BatchItem,WorkMateLib.BatchLib.BatchItem]' to type 'System.Collections.Generic.List`1[WorkMateLib.BatchLib.BatchItem]'.
请你复习一下,告诉我哪里出了问题。 以下是这些类的代码:

Namespace BatchLib
    Public Class BatchItem
        Dim _BatchID As String
        Dim _BatchName As String
        Dim _Category As String
        Dim _CourseID As String
        Dim _CourseName As String
        Dim _StartDate As Date
        Dim _StartDateFormated As String
        Dim _Deleted As Boolean
#Region "Property"
        Property BatchID
            Get
                Return _BatchID
            End Get
            Set(value)
                _BatchID = value
            End Set
        End Property
        Property BatchName
            Get
                Return _BatchName
            End Get
            Set(value)
                _BatchName = value
            End Set
        End Property
        Property Category
            Get
                Return _Category
            End Get
            Set(value)
                _Category = value
            End Set
        End Property
        Property CourseID
            Get
                Return _CourseID
            End Get
            Set(value)
                _CourseID = value
            End Set
        End Property
        Property DateStarted
            Get
                Return _StartDate
            End Get
            Set(value)
                _StartDate = value
            End Set
        End Property
        Property Deleted
            Get
                Return _Deleted
            End Get
            Set(value)
                _Deleted = value
            End Set
        End Property
        Property CourseName
            Get
                Return _CourseName
            End Get
            Set(value)
                _CourseName = value
            End Set
        End Property
        Property StartDateFormated
            Get
                Return _StartDateFormated
            End Get
            Set(value)
                _StartDateFormated = value
            End Set
        End Property
#End Region
    End Class

    Public Class Batchs
        Public BatchList As New List(Of BatchItem)
        Public Function Contains(ByVal Batchname As String, ByVal CourseID As String)
            For Each k As BatchItem In BatchList
                If k.CourseID = CourseID And k.BatchName = Batchname Then
                    Return True
                    Exit Function
                End If
            Next
            Return False
        End Function
        Public Function Contains(ByVal Batchname As String, ByVal CourseID As String, ByVal BatchID As String)
            For Each k As BatchItem In BatchList
                If k.CourseID = CourseID And k.BatchName = Batchname Then
                    If k.BatchID = BatchID Then
                    Else
                        Return True
                        Exit Function
                    End If
                End If
            Next
            Return False
        End Function
    End Class
End Namespace

谢谢你的努力。我是linq的新手,所以我可能不太擅长,非常欢迎你的助手让我理解这个错误。

例外确实说明了你的问题所在。Linq表达式不返回
列表
,并且它不可分配给
列表
。尝试将整个表达式括在括号中,然后使用
ToList()

是的,这很有效。但我能知道为什么它不能在早些时候工作吗?因为我写的查询是正确的,我相信它一定只返回一个列表。问题不在查询中,而在赋值运算符(
=
)中。所以,给出列表的查询不能直接分配给列表类型,它需要明确定义为列表类型,这是内部编程的吗?好的谢谢你提供的信息。