假设我有一个表,例如:
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 的 child ,C 是 B 的 child ,依此类推。
我怎样才能将表顶部变成分层数据源? 比如
ID ParentID
1D 1C
4D 2C
6D 1C
9D 1C
8D 2C
1C 1B
2C 6B
1B 1A
6B 1A
1A Null
这可以用作 Telerik TreeView 或其他分层控件的分层数据源吗? 我知道我可以迭代每个项目并自己构建它,但我想知道是否有更好的方法来迭代它。甚至可能是实现这一目标的内置方式。
最佳答案
您可以编写一个简单的迭代来遍历文件并将元素对添加到字典中。在看起来像 python 的伪代码中:
// open the file you want to parse.
file = open(my_log_file)
// create a hash map from ID to parent.
dictionary = {}
// read through the file line by line
for line in file.getlines():
// ignore first line ...
// read the 4 columns in each line
columns[] = line.split(" ")
// add pairs (id=column[i], parent=column[i+1]) to the hashmap
dictionary[column[1]] = column[2]
dictionary[column[2]] = column[3]
dictionary[column[3]] = column[4]
dictionary[column[4]] = Nil
// output the hashmap line by line.
print "ID", "Parent ID"
for (id, parent) in dictionary:
print id, parent
希望对您有所帮助。
关于.net - 将扁平化数据源转换为分层数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4467141/