.net 将扁平化数据源转换为层次化数据源
假设我有一张桌子,比如:.net 将扁平化数据源转换为层次化数据源,.net,algorithm,.net-3.5,datasource,hierarchy,.net,Algorithm,.net 3.5,Datasource,Hierarchy,假设我有一张桌子,比如: 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的孩子,等等 我怎样才能将表转换为分层
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的孩子,等等
我怎样才能将表转换为分层数据源?
比如
这可以作为Telerik TreeView或其他分层控件的分层数据源?
我知道我可以迭代每个项目并自己构建它,但我想知道是否有更好的迭代方法。甚至可能是一种内置的方法来实现这一点。您可以编写一个简单的迭代来遍历文件,并将成对的元素添加到字典中。在pythonish外观的psuedo代码中:
// 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
我希望这有帮助。这里有一个快速的想法 如果我理解正确的话,你可以从右到左遍历列表,然后读对。。。使用字典/地图/哈希表将它们组织起来。您可能需要为它定义一个自定义比较器,但这并不难 因此,当您从右到左、从上到下迭代时,您可以在读取这些对的同时添加它们
parent = "1A";
child = "1B";
list.add(child, parent);
//Then on the next iteration.....
parent = "1B";
child = "1C";
等你去
通过使用子项作为键,您将得到所有唯一值的列表,这将成为您的ID列
然后,list.values()将成为ParentID列,因为它将包含具有多个子项的所有父项的重复项。这也是为什么孩子可以完美地作为钥匙,因为你只能出生一次,但你可以有很多孩子
编辑:呸!有人用我提议的更完整的版本打败了我。。。哦,好吧。我更喜欢我的口头描述;)
parent = "1A";
child = "1B";
list.add(child, parent);
//Then on the next iteration.....
parent = "1B";
child = "1C";