更有效地构建树? [英] Build Tree more efficiently?

查看:96
本文介绍了更有效地构建树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



基本上,我填充了一个TreeView,列出了我的数据库中的所有部门。以下是实体框架模型:





以下代码有问题:

  private void button1_Click对象发件人,EventArgs e)
{
DepartmentRepository repo = new DepartmentRepository();
var parentDepartments = repo.FindAllDepartments()
.Where(d => d.IDParentDepartment == null)
.ToList();
foreach(parent parentDepartments中的var parent)
{
TreeNode node = new TreeNode(parent.Name);
treeView1.Nodes.Add(node);

var children = repo.FindAllDepartments()
.Where(x => x.IDParentDepartment == parent.ID)
.ToList();
foreach(孩子中的var child)
{
node.Nodes.Add(child.Name);
}
}
}

编辑: / strong>



到目前为止的好建议。使用整个集合是有道理的我猜。但是如果收藏是巨大的,如20万条目,会发生什么?这不会破坏我的软件吗?

  DepartmentRepository repo = new DepartmentRepository(); 
var entries = repo.FindAllDepartments();

var parentDepartments = entries
.Where(d => d.IDParentDepartment == null)
.ToList();
foreach(parent parentDepartments中的var parent)
{
TreeNode node = new TreeNode(parent.Name);
treeView1.Nodes.Add(node);

var children = entries.Where(x => x.IDParentDepartment == parent.ID)
.ToList();
foreach(孩子中的var child)
{
node.Nodes.Add(child.Name);
}
}


解决方案

对我来说看起来不错,但想想收集数十万个节点。最好的方法是异步加载 - 请注意,这并不意味着必须同时加载所有元素。您的树视图可以默认折叠,您可以在用户展开树的节点时加载其他级别。我们来考虑这种情况:你有一个根节点包含100个节点,每个节点包含至少1000个节点。 100 * 1000 = 100000个节点加载 - 几乎不是吗?为了减少数据库流量,您可以首先加载前100个节点,然后在用户扩展其中一个节点时,可以加载其1000个节点。这将节省大量的时间。


I was wondering if this code is good enough or if there are glaring newbie no-no's.

Basically I'm populating a TreeView listing all Departments in my database. Here is the Entity Framework model:

Here is the code in question:

private void button1_Click(object sender, EventArgs e)
{            
    DepartmentRepository repo = new DepartmentRepository();
    var parentDepartments = repo.FindAllDepartments()
                          .Where(d => d.IDParentDepartment == null)
                          .ToList();
    foreach (var parent in parentDepartments)
    {           
        TreeNode node = new TreeNode(parent.Name); 
        treeView1.Nodes.Add(node);

        var children = repo.FindAllDepartments()
                     .Where(x => x.IDParentDepartment == parent.ID)
                     .ToList();
        foreach (var child in children)
        {
            node.Nodes.Add(child.Name);                       
        }                
    }
}

EDIT:

Good suggestions so far. Working with the entire collection makes sense I guess. But what happens if the collection is huge as in 200,000 entries? Wouldn't this break my software?

DepartmentRepository repo = new DepartmentRepository();
var entries = repo.FindAllDepartments();

var parentDepartments = entries
                      .Where(d => d.IDParentDepartment == null)
                      .ToList();
foreach (var parent in parentDepartments)
{
    TreeNode node = new TreeNode(parent.Name);
    treeView1.Nodes.Add(node);

    var children = entries.Where(x => x.IDParentDepartment == parent.ID)
                          .ToList();
    foreach (var child in children)
    {
        node.Nodes.Add(child.Name);
    }
}

解决方案

That looks ok to me, but think about a collection of hundreds of thousands nodes. The best way to do that is asynchronous loading - please notice, that do don't necassarily have to load all elements at the same time. Your tree view can be collapsed by default and you can load additional levels as the user expands tree's nodes. Let's consider such case: you have a root node containing 100 nodes and each of these nodes contains at least 1000 nodes. 100 * 1000 = 100000 nodes to load - pretty much, istn't it? To reduce the database traffic you can first load your first 100 nodes and then, when user expands one of those, you can load its 1000 nodes. That will save considerable amount of time.

这篇关于更有效地构建树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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