Nic*_*ick 2 vb.net linq-to-objects
我有一个自定义对象的通用列表,并希望将该列表减少到特定属性值不在排除列表中的对象.
我尝试过以下方法:
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
Run Code Online (Sandbox Code Playgroud)
但得到错误:
System.InvalidCastException:无法转换类型为'WhereListIterator
1[MyApp.Classes.Data.Entities.Template]' to type 'System.Collections.Generic.List1 [MyApp.Classes.Data.Entities.Template]'的对象.
如何仅选择模板ID不在排除列表中的模板?
将ToList()扩展名添加到Where扩展名的末尾,以将其转换回相应类型的List.
If usedTemplates IsNot Nothing Then
addIns = addIns.Where(Function(a) usedTemplates.Contains(a.TemplateID) = False) _
.ToList()
End If
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5084 次 |
| 最近记录: |