2009-04-24 84 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

粘性一個ToList()擴展到哪裏末端延伸至其轉換回相應類型的列表。

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

點上,謝謝! :d – Nick 2009-04-24 15:22:01