Reputation: 1422
For a long time, I have been try to do this:
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
But is keep on throwing the following exception:
Unable to cast object of type 'WhereSelectListIterator`2[WorkMateLib.BatchLib.BatchItem,WorkMateLib.BatchLib.BatchItem]' to type 'System.Collections.Generic.List`1[WorkMateLib.BatchLib.BatchItem]'.
Could you please review and let me know where I am going wrong please. Following is the code for the classes:
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
Thank you for your efforts. I am new to linq, so I may not be that good at it, your assistant and making me understand the mistake is heartily welcomed.
Upvotes: 0
Views: 610
Reputation: 3681
Exception does say exacly whats ur problem. Linq expression does not return List
and its not assignable to List
. Try surounding your whole expression in brackets and use ToList()
Upvotes: 1