链接异常/错误

时间:2011-07-05 11:01:15

标签: 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.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的新手,所以我可能不那么擅长,你的助手让我理解这个错误是受到了热烈的欢迎。

1 个答案:

答案 0 :(得分:1)

例外确实说你的问题是什么。 Linq表达式不返回List,并且不能分配给List。尝试用括号括起你的整个表达式并使用ToList()