2009-04-24 13 views
2

私はカスタムオブジェクトの一般的なリストを持っており、そのリストを特定のプロパティ値が除外リストにないオブジェクトに還元したいと考えています。オブジェクトへのLINQ - ではありませんか?

私は次のことを試してみました:

Private Sub LoadAddIns() 
    // Get add-in templates 
    Dim addIns = GetTemplates(TemplateTypes.AddIn) 
    // Get the current document 
    Dim sectionId As String = CStr(Request.QueryString("sectionId")) 
    Dim docId As Integer = CInt(Split(sectionId, ":")(0)) 
    Dim manual = GetTempManual(docId) 
    Dim content As XElement = manual.ManualContent 
    // Find which templates have been used to create this document. 
    Dim usedTemplates = (From t In content.<header>.<templates>.<template> _ 
         Select CInt(t.<id>.Value)).ToList 
    // Exclude add-ins that have already been used. 
    If usedTemplates IsNot Nothing Then 
    addIns = addIns.Where(Function(a) usedTemplates.Contains(a.TemplateID) = False) 
    End If 
    // Bind available add-ins to dropdown 
    With ddlAddIns 
    .DataSource = addIns 
    .DataTextField = "Title" 
    .DataValueField = "TemplateID" 
    .DataBind() 
    .Items.Insert(0, New ListItem("[select an add-in]", 0)) 
    End With 
End Sub 

が、エラーが出ます:

System.InvalidCastException: Unable to cast object of type 'WhereListIterator 1[MyApp.Classes.Data.Entities.Template]' to type 'System.Collections.Generic.List 1[MyApp.Classes.Data.Entities.Template]'.

は、どのように私はテンプレートIDは、除外リストにないテンプレートのみを選択することができますか?

答えて

5

To拡張子の末尾にToList()拡張を追加して、適切な型のリストに戻します。

If usedTemplates IsNot Nothing Then 
    addIns = addIns.Where(Function(a) usedTemplates.Contains(a.TemplateID) = False) _ 
        .ToList() 
End If 
+0

ありがとう! :D – Nick

+0

ありがとうトン! C#で同じ問題があった場合、ToList()がキーでした! –

関連する問題