具有深度的递归层次连接 [英] Recursive Hierarchical Joins with Depth

查看:120
本文介绍了具有深度的递归层次连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照 C#和LINQ 中的递归层次连接,以实现递归连接扩展以在我的应用程序中显示树视图数据。由于我有15000个树节点,jQuery DynaTree 在客户端准备需要花费很多时间(50秒)在错误的浏览器IE 7& 8

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;

    }

我尝试设置深度


深度= deepLevel

但没有用。可能是什么问题?我怎样才能做到这一点?

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

推荐答案

RecursiveJoin扩展方法用于从原始节点构建树。
您使用的重载已传递int深度参数,初始化新创建的节点,其中将包含它所在的级别,并且与构建的树的深度无关。

RecursiveJoin extension method is used to build a tree out of raw nodes. The overload you are using has int depth parameter passed, to initialize new created node with level at which it will reside, and has nothing to do with depth of the tree which is built.

扩展本身是懒惰的。当你第一次调用RecursiveJoin时,它将返回根的IEnumerable<>,直到你开始枚举才实现。

Extension by itself is lazy. When you first invoke RecursiveJoin, it will return IEnumerable<> of roots, which is not materialized until you start enumerating.

你的代码问题是你急切地转换IEnumerable< >将孩子列入清单:

Problem with your code is that you eagerly convert IEnumerable<> of children into list:


children = childNodes.ToList()

children = childNodes.ToList()

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

This forces materialization of children collection, recursively calls same method again to construct DynaTreeNode, and repeats for all levels. Try to replace children list in DynaTreeNode with IEnumerable - this will produce DynaTreeNodes on demand.

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

Or you can keep list, and replace line above with following code:


chidren = depth> = deepLevel? null:childNodes.ToList()

chidren = depth >= deepLevel ? null : childNodes.ToList()

这将按预期切断传递级别的树。

This will cut off tree at the passed level, as expected.

这篇关于具有深度的递归层次连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆