Vb.net 获取树中具有特定字段值的元素列表
我有这样一个界面:Vb.net 获取树中具有特定字段值的元素列表,vb.net,linq,properties,interface,Vb.net,Linq,Properties,Interface,我有这样一个界面: Public Interface TreeSelectorAttributes Property selectedInTreeSelector As Boolean Property Name As String ReadOnly Property childs As IEnumerable(Of TreeSelectorAttributes) End Interface 我有一个树状视图,上面有一个树状列表。选秀嘉宾致意: Public Prope
Public Interface TreeSelectorAttributes
Property selectedInTreeSelector As Boolean
Property Name As String
ReadOnly Property childs As IEnumerable(Of TreeSelectorAttributes)
End Interface
我有一个树状视图,上面有一个树状列表。选秀嘉宾致意:
Public Property rootList As IEnumerable(Of TreeSelectorAttributes)
现在,在用户选择要选择的元素和不想选择的元素之后,我希望能够返回所有选定元素的树,但此属性仅返回第一层元素:
Public ReadOnly Property checkedList As List(Of TreeSelectorAttributes)
Get
Return (From ele As TreeSelectorAttributes
In rootList
Where ele.selectedInTreeSelector = True).ToList()
End Get
End Property
如何仅返回此树/列表中选定的子元素
正如在评论中指出的,我无法更改childs(只读) 所以我的想法是在界面中有一个属性“selectedChilds” 可能吗? 我看到的问题是,在接口中,我不能直接实现属性,我不喜欢看到的其他选项: 有一个抽象类,其实现属性selectedChilds->我不喜欢这样,因为如果我每次都这样做。。。
每次我实现接口时自己实现属性->我不喜欢这样,因为我将使用CodeClones而不是CodeClones://如果我理解正确,您希望获得所有选定的父级和所有选定的子级。您可以使用递归方法:
Public ReadOnly Property checkedList As List(Of TreeSelectorAttributes)
Get
Return rootList.Where(Function(t) t.SelectedInTreeSelector).
SelectMany(Function(root) GetSelectedChildren(root)).
ToList()
End Get
End Property
Function GetSelectedChildren(root As TreeSelectorAttributes, Optional includeRoot As Boolean = True) As List(Of TreeSelectorAttributes)
Dim allSelected As New List(Of TreeSelectorAttributes)
If includeRoot Then allSelected.Add(root)
Dim childTrees As New Queue(Of TreeSelectorAttributes)
childTrees.Enqueue(root)
While childTrees.Count > 0
Dim selectedChildren = From c In childTrees.Dequeue().Children
Where c.SelectedInTreeSelector
For Each child In selectedChildren
allSelected.Add(child)
childTrees.Enqueue(child)
Next
End While
Return allSelected
End Function
此方法使用a来支持理论上的无限深度。树有多深,只有一级子级?您想返回一个
列表(树选择者的贡献)
其中包含同一级别上的所有父级和子级,或者仅包含所有子级的selectedTreeSelector
为true
的父级?如果您只想返回包含childs
且仅包含TreeSelectorAttributes
且也包含selectedTreeSelector=true
的父级不起作用,因为childs
是ReadOnly
@TimSchmelter我不知道有多少子级别,我想要通用的。在我当前的用例中,我有3个Levels@TimSchmelter是的,但我改变了问题的答案;)我想在评论之后告诉你:哦,不,我没有。。。sry。我想把它们放在树上而不是列表上都在同一个lvlYou想让它们放在树上而不是列表上?但是您的属性返回一个列表(树选择器属性)
。