生成数据集的父母,孩子的名单(递归关系) [英] Generate list of parents-childs (recursive relationship) from Dataset
问题描述
我试图建立与递归关系的列表(HTML)。该数据是在数据集中,但可以转换为一个数据表,如果它更容易
我不知道什么是实现这一目标的最佳选择。我想使用嵌套的中继器。
下面的数据:
__ ID__ | __name__ | __parent__ | __水平__
1 |帕特里克| | 1
2 |马克| | 1
3 |斯科特| 2 | 2
4 |杰森| | 1
5 |朱利安| | 1
6 |约翰| 6 | 2
7 |史蒂夫| | 1
8 |乔治| 1 | 2
9 |罗伯特| 1 | 2
10 |罗德尼| 8 | 3
下面的输出我想产生
- 帕特里克[1]
- 乔治[8]
- 罗德尼[10]
- 罗伯特[9]马克 - [2]
- 斯科特[3] - 朱[5]
约翰 - [6]贾森 - [4]史蒂夫 - [7]
要做到这一点,最简单的方法是写一个递归方法。它的运作将取决于您是否想拥有的方法返回的方式对整个树结构列表,或输出数据,因为它读取它。如果你要输出的数据,你读它,你的code可能是这个样子:
私人小组OutputTree(数据作为数据表,parentId的作为字符串,indentationLevel作为整数)
对于每一行作为的DataRow在GetChildRows(parentId的)
OutputRow(行,indentationLevel)
OutputTree(数据,行(ID)。的ToString(),indentationLevel + 1)的
下一个
结束小组
以上code假定您还实现一个名为 GetChildRows
方法,它返回所有包含给定父ID行的列表。它还假定你有一个名为方法 OutputRow
其中给定行给定的缩进级别输出。
然后,你可以这样调用方法:
OutputTree(myDataTable,什么都没有,0)
如果你想建立并返回一个树状结构,这无疑是更好的方法,你的code可能是这个样子:
专用功能BuildTreeNodes(数据作为数据表,parentId的作为字符串)作为列表(中MyTreeNode)
)昏暗的节点作为列表(OfNew MyTreeNode)(
对于每一行作为的DataRow在GetChildRows(parentId的)
昏暗的节点作为新的TreeNode()
node.Row =行
node.Children = BuildTreeNodes(数据,联排(ID)。toString()方法)
nodes.Add(节点)
下一个
返回节点
结束小组
以上code假定您已经定义了一个 MyTreeNode
类,它会是这个样子:
公共类MyTreeNode
公共属性行作为的DataRow
公共财产儿童名单(中MyTreeNode)
末级
然后,你可以调用该方法是这样的:
昏暗rootLevelNodes方式列表(中MyTreeNode)= BuildTreeNodes(myDataTable,为Nothing)
I'm trying to build a list (HTML) with a recursive relationship. The data is in a dataset but could converted to a data table if it's easier.
I don't know what's the best option to achieve this. I was thinking about using nested repeaters.
Here's the data:
__ID__ | __NAME__ | __PARENT__ | __LEVEL__
1 | Patrick | | 1
2 | Mark | | 1
3 | Scott | 2 | 2
4 | Jason | | 1
5 | Julian | | 1
6 | John | 6 | 2
7 | Steve | | 1
8 | George | 1 | 2
9 | Robert | 1 | 2
10 | Rodney | 8 | 3
Here the output I want to produce
- Patrick [1]
- George [8]
- Rodney [10]
- Robert [9]
- Mark [2]
- Scott [3]
- Julian [5]
- John [6]
- Jason [4]
- Steve [7]
The easiest way to do this is to write a recursive method. The way it operates will depend on whether you want to have the method return the entire tree-structured list, or output the data as it reads it. If you want to output the data as you read it, your code might look something like this:
Private Sub OutputTree(data As DataTable, parentId As String, indentationLevel As Integer)
For Each row As DataRow In GetChildRows(parentId)
OutputRow(row, indentationLevel)
OutputTree(data, row("ID").ToString(), indentationLevel + 1)
Next
End Sub
The above code assumes that you also implement a method called GetChildRows
which returns a list of all the rows that contain the given parent ID. It also assumes that you have a method called OutputRow
which outputs the given row at the given indentation level.
Then, you could call the method like this:
OutputTree(myDataTable, nothing, 0)
If you want to build and return a tree structure, which is arguably the better approach, your code might look something like this:
Private Function BuildTreeNodes(data As DataTable, parentId As String) As List(Of MyTreeNode)
Dim nodes As List(OfNew MyTreeNode)()
For Each row As DataRow In GetChildRows(parentId)
Dim node As New TreeNode()
node.Row = row
node.Children = BuildTreeNodes(data, row("ID").ToString())
nodes.Add(node)
Next
Return node
End Sub
The above code assumes that you have defined a MyTreeNode
class which would look something like this:
Public Class MyTreeNode
Public Property Row As DataRow
Public Property Children As List(Of MyTreeNode)
End Class
Then you could call the method like this:
Dim rootLevelNodes As List(Of MyTreeNode) = BuildTreeNodes(myDataTable, Nothing)
这篇关于生成数据集的父母,孩子的名单(递归关系)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!