2

我有一个自定义对象的通用列表,并希望将该列表减少为特定属性值不在排除列表中的对象。

我尝试了以下方法:

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:无法转换类型为“WhereListIterator 1[MyApp.Classes.Data.Entities.Template]' to type 'System.Collections.Generic.List1 [MyApp.Classes.Data.Entities.Template]”的对象。

如何仅选择模板 ID 不在排除列表中的模板?

4

1 回答 1

5

将 ToList() 扩展添加到 Where 扩展的末尾,以将其转换回适当类型的列表。

If usedTemplates IsNot Nothing Then
    addIns = addIns.Where(Function(a) usedTemplates.Contains(a.TemplateID) = False) _
                   .ToList()
End If
于 2009-04-24T15:05:59.260 回答