2011-07-05 12 views
0

、私はこれをしようとされています:リンク例外/エラー

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

例外はexacly whats urの問題を言っています。 Linq式はListを返しません。Listに割り当てられません。角括弧の中にあなたの全体の表現を囲み、ToList()

+0

うまくいってください。しかし、私が書いた質問が正しいと思っていて、リストだけを返す必要があるので、それが以前にはうまくいかなかった理由を知ることができますか? – surpavan

+1

問題はクエリではなく、代入演算子( '=')です。 –

+0

リストを与えるクエリは、リスト型に直接代入することはできません。リスト型に明確に定義する必要があります。ファイン。情報のおかげで。 – surpavan