将扁平化数据源转变为分层数据源
假设我有一个表,例如:
D_ID C_ID B_ID A_ID
1D 1C 1B 1A
4D 2C 6B 1A
6D 1C 1B 1A
9D 1C 1B 1A
8D 2C 6B 1A
假设从结构上来说,我知道以下内容:
A's
B's
C's
D's
即 D 是 C 的子级,C 是 B 的子级,依此类推。
我怎样才能将顶部的表变成分层数据源? 例如,
ID ParentID
1D 1C
4D 2C
6D 1C
9D 1C
8D 2C
1C 1B
2C 6B
1B 1A
6B 1A
1A Null
这可以作为 Telerik TreeView 或其他分层控件的分层数据源吗? 我知道我可以迭代每个项目并自己构建它,但我想知道是否有更好的方法来迭代它。也许甚至有一种内置的方式来实现这一目标。
Let's say I have a table such as:
D_ID C_ID B_ID A_ID
1D 1C 1B 1A
4D 2C 6B 1A
6D 1C 1B 1A
9D 1C 1B 1A
8D 2C 6B 1A
And let's say that structurally speaking, I know the following:
A's
B's
C's
D's
That is D's are children of C's, C's of B's and so on.
How could I turn the table up top into a hierarchical data source?
Such as
ID ParentID
1D 1C
4D 2C
6D 1C
9D 1C
8D 2C
1C 1B
2C 6B
1B 1A
6B 1A
1A Null
This could then serve as a hierarchical datasource for a Telerik TreeView or other hierarchical controls?
I know I could iterate over every item and build it myself, but I am wondering if there are better known ways to iterate this. Perhaps even a built in way to achieve this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以编写一个简单的迭代来遍历文件并将元素对添加到字典中。在看起来像Python的伪代码中:
我希望这会有所帮助。
You could write a simple iteration passing through the file and adding pairs of elements to a dictionary. In pythonish looking psuedo code:
I hope this helps.
这是一个快速的想法...
如果我正确理解这一点,您可以从右到左迭代列表,并读取对...使用字典/地图/哈希表来组织它们。您可能需要为其定义一个自定义比较器,但这并不太难。
因此,当您从右到左、从上到下迭代时,您在阅读时添加对,如此......
依此类推......
这样使用子项作为键,您将得到所有的列表唯一值,这将成为您的 ID 列。
然后你的 list.values() 就成为你的 ParentID 列,因为它将包含所有拥有多个孩子的父母的重复项。这也是为什么孩子是完美的关键,因为你只能出生一次,但你可以有很多孩子。
编辑:呸!无论如何,有人用我所提议的更完整的版本击败了我……哦,好吧。我更喜欢我的口头描述;)
Here is a quick thought....
If I understand this correctly you can just iterate across the list from right to left, and read pairs... Use a dictionary/map/hashtable to keep them organized. You may need to define a custom comparator for it but thats not too hard.
So as you iterate along from right to left, top to bottom, you add the pairs as you read them so..
And so on you go...
This way using the child as the key, you are left with a list of all the unique values, this becomes your ID column.
Then your list.values() becomes your ParentID column since it will contain the duplicates for all of the parents that had more than one child. This is also why the child works perfectly as the key since you can only be born once, but you can have many children.
EDIT: BAH! Someone beat me to it with a much more complete version of what I was proposing anyway... oh well. I like my verbal description better ;)