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

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

问题描述

我在想,如果这个代码是不够好,或者有怒视新手禁忌的。



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





下面是有问题的代码:

 私人无效的button1_Click(对象发件人,EventArgs五)
{
DepartmentRepository回购=新DepartmentRepository();
VAR parentDepartments = repo.FindAllDepartments()
。凡(D => d.IDParentDepartment == NULL)
.ToList();
的foreach(在parentDepartments VAR父)
{
树节点节点=新的TreeNode(parent.Name);
treeView1.Nodes.Add(节点);

VAR孩子= repo.FindAllDepartments()
。凡(X => x.IDParentDepartment == parent.ID)
.ToList();
的foreach(VAR的孩子的孩子)
{
node.Nodes.Add(child.Name);
}
}
}



编辑:



好建议为止。与整个收集工作很有意义,我猜。但是,会发生什么,如果集合是巨大的200,000项? ?这是不是打破我的软件



  DepartmentRepository回购=新DepartmentRepository(); 
VAR项= repo.FindAllDepartments();

VAR parentDepartments =输入
。凡(D => d.IDParentDepartment == NULL)
.ToList();
的foreach(在parentDepartments VAR父)
{
树节点节点=新的TreeNode(parent.Name);
treeView1.Nodes.Add(节点);

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


解决方案

这看起来不错给我,但是想想成千上万节点的集合。要做到这一点,最好的方法是异步加载 - 请注意,那些不necassarily必须加载在同一时间的所有元素。您的树视图可以被默认倒塌,为用户展开树的节点就可以加载其他级别。让我们考虑这种情况:有一个包含100个节点根节点和每个节点含有至少1000个节点。 100 * 1000 = 100000节点加载 - 好看多了,istn't呢?为了减少数据库流量可以先加载你的第一个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天全站免登陆