总结树上的值

发布于 2024-11-02 01:06:53 字数 251 浏览 8 评论 0原文

我使用树控件来查看一些基于嵌套(父子)表的分层项目。

每个节点都有一个 NameValue 格式,接受 name 和 value 。

但只有叶子(最后一个节点)具有整数值,并且父节点的值保留为空(仅是它们具有的名称)。

我想汇总值,以便每个父节点都保存其子节点和叶子值的总和。

我认为需要递归或者 LINQ 来完成这项任务,但我不知道如何实现?

也许一些伪代码对我有帮助。

预先感谢您的帮助!

I used a Tree control to view some hierarchical items base on a nested (parent child) table .

Every node has a NameValue format that accept either a name and value .

But only Leaves (last nodes) have integer values and values of parents are left blank (just the Names they have) .

I want to summarize values so that every parent hold the sum of it's sub nodes and leaves values .

I think recursion or maybe LINQ is needed to accomplish this task but i don't know how ?

maybe some pseudo code will be helpful for me .

Thanks in advance for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七度光 2024-11-09 01:06:53

这是未经测试的,但我认为设置所有节点的所有值可能有效:

public void SetNodeValues(Node node)
{
    if (node.Name == String.Empty)
    {
        //If it has no name it is a leaf, which needs no value
        return;
    }
    else
    {
        //Make sure all child-nodes have values
        foreach (var childNode in node.ChildNodes)
        {
            SetNodeValues(childNode);
        }

        //Sum them up and set that as the current node's value
        node.Value = node.ChildNodes.Sum(x => x.Value);
    }
}

This is untested but i think it might work to set all the values of all nodes:

public void SetNodeValues(Node node)
{
    if (node.Name == String.Empty)
    {
        //If it has no name it is a leaf, which needs no value
        return;
    }
    else
    {
        //Make sure all child-nodes have values
        foreach (var childNode in node.ChildNodes)
        {
            SetNodeValues(childNode);
        }

        //Sum them up and set that as the current node's value
        node.Value = node.ChildNodes.Sum(x => x.Value);
    }
}
秋日私语 2024-11-09 01:06:53

这将为您完成:

class Node
{
    public Node()
    {
        Children = new List<Node>();
    }

    public IEnumerable<Node> GetSubTree()
    {
        return Children.SelectMany(c => c.GetSubTree()).Concat(new[] { this });
    }

    public List<Node> Children { get; set; }
    public string Value { get; set; }
}

class Tree
{
    public Tree()
    {
        Root = new Node();
    }

    public IEnumerable<Node> GetAllNodes()
    {
        return Root.Children.SelectMany(root => root.GetSubTree()); 
    }

    Node Root { get; set; }

    //This is the Property you want:
    public int GetValuesSum
    {
        get
        {
            return GetAllNodes().Where(node => !string.IsNullOrEmpty(node.Value)).Sum(node => Convert.ToInt32(node.Value));
        }
    }
}

参考:如何使用 LINQ 从树中的所有节点获取列表?

This will do it for you :

class Node
{
    public Node()
    {
        Children = new List<Node>();
    }

    public IEnumerable<Node> GetSubTree()
    {
        return Children.SelectMany(c => c.GetSubTree()).Concat(new[] { this });
    }

    public List<Node> Children { get; set; }
    public string Value { get; set; }
}

class Tree
{
    public Tree()
    {
        Root = new Node();
    }

    public IEnumerable<Node> GetAllNodes()
    {
        return Root.Children.SelectMany(root => root.GetSubTree()); 
    }

    Node Root { get; set; }

    //This is the Property you want:
    public int GetValuesSum
    {
        get
        {
            return GetAllNodes().Where(node => !string.IsNullOrEmpty(node.Value)).Sum(node => Convert.ToInt32(node.Value));
        }
    }
}

Reference : How can I get a List from all nodes in a tree using LINQ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文