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