2

I followed this Recursive Hierarchical Joins in C# and LINQ post to implement recursive join extension to show a tree view data in my application. As i have 15000 tree nodes, the jQuery DynaTree at client side preparation takes lot of time (50 Secs) in buggy browser I.E 7&8

To avoid this i decided to load only one level initially, then load other children on demand (Lazy Loading).

But i could not see the effect in setting depth in recursive join. Even i specify, it prepare all nodes.

public static List<DynaTreeNode> GenerateTreeByDepth(List<NodeDTO> nodeList, 
   int deepLevel)
  {

  StringBuilder hirTree = new StringBuilder();
  List<DynaTreeNode> tree = new List<DynaTreeNode>();

  IEnumerable<DynaTreeNode> nodes = nodeList.RecursiveJoin
  (element => element.DataPointSK,element => element.DataPointSKParent,
  (NodeDTO element, int index, int depth,IEnumerable<DynaTreeNode> childNodes) => 
                new DynaTreeNode()
               {
                   title = element.DataPoint,
                   key = element.DataPointSK.ToString(),
                   children = childNodes.ToList(),
                   select = element.selected,
                   expand = element.selected,                      
                   Depth = deepLevel
               });
        tree = nodes.ToList();          
        return tree;

    }

I tried setting depth

Depth = deepLevel

But no use. What could be the issue? How can i do this?

4

1 回答 1

0

RecursiveJoin 扩展方法用于从原始节点构建树。您正在使用的重载传递了 int depth 参数,以初始化新创建的节点及其所在的级别,并且与构建的树的深度无关。

扩展本身是惰性的。当您第一次调用 RecursiveJoin 时,它将返回根的 IEnumerable<>,在您开始枚举之前不会具体化。

您的代码的问题是您急切地将 IEnumerable<> 的孩子转换为列表:

孩子 = childNodes.ToList()

这会强制子集合的具体化,再次递归调用相同的方法来构造 DynaTreeNode,并在所有级别重复。尝试用 IEnumerable 替换 DynaTreeNode 中的子列表 - 这将按需生成 DynaTreeNode。

或者您可以保留列表,并将上面的行替换为以下代码:

孩子=深度>=深度?null : childNodes.ToList()

正如预期的那样,这将在通过的级别切断树。

于 2013-03-28T22:13:03.290 回答