Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 计算列表中父实体的子对象之和_C#_List_Sum_Parentid - Fatal编程技术网

C# 计算列表中父实体的子对象之和

C# 计算列表中父实体的子对象之和,c#,list,sum,parentid,C#,List,Sum,Parentid,我的列表结构如下: public class Custo{ public int id { get; set; } public int idParent { get; set; } public decimal amount{ get; set; } public decimal unitaryValue{ get; set; } } List<Custo> myLIst = new List<Custo>();

我的列表结构如下:

public class Custo{
      public int id { get; set; }
      public int idParent { get; set; }
      public decimal amount{ get; set; }
      public decimal unitaryValue{ get; set; }

}

List<Custo> myLIst  = new List<Custo>();
Name              Total Amount      Total Value

Projetos           17,00            $70 

  Arquitetura      15,00            $60

    Estrutura      10,00            $35
    Modulo          5,00            $25

  Desenho           2,00            $10  
    Artistico       2,00            $10  

Projetos Eletricos  0,00            $0

基本上,在复合模式中,需要三个类。一个是treenode,另外两个将继承它。一个是能够容纳儿童的项目。另一个是子节点/叶节点,是单独的CUSTO

使用这些类时,您将创建一个新的Projectos并添加子节点,然后在需要显示它们时调用Display方法。显然,您可以为其他函数添加逻辑。这与此处的代码类似:

用法可能是 Main

Projectos project = new Projectos("Projetos");
project.Add(new Custos("Arquitetura", 15, 70.00);

Projectos electricos = new Projectos("Electricos");
electricos.Add(new Custos("custo1", 20, 80.00);
project.Add(electricos);

project.Display();
Projectos

using System;
using System.Collections.Generic;

namespace CompositePatternExercise
{
    class Projectos : CustosNode
    {
        private List<CustosNode> elements = new List<CustosNode>();

        // Constructor
        public Projectos(string name) : base(name)
        {
        }

        public override void Add(CustosNode d)
        {
            elements.Add(d);
        }

        public override void Remove(CustosNode d)
        {
            elements.Remove(d);
        }
        public override void Display()
        {
            decimal totalValue = 0;
            decimal totalAmount = 0;

            Console.WriteLine(_name);

            // Display each child element on this node
            foreach (CustosNode d in elements)
            {
                d.Display();
                totalAmount += d.Amount;
                totalValue += d.Value;
            }

            Console.Write("Project total:" + totalAmount.ToString("C") + " " + totalAmount.ToString("C"));

        }
    }
}
namespace CompositePatternExercise
{
    class Custos : CustosNode
    {
            // Constructor
            public Custos(string name, decimal amount, decimal value) : base(name, amount, value)
            {
            }
            public override void Add(CustosNode c)
            {
                Console.WriteLine(
                  "Cannot add to a custo");
            }
            public override void Remove(CustosNode c)
            {
                Console.WriteLine(
                  "Cannot remove from a custo");
            }
            public override void Display()
            {
                Console.WriteLine(_name + " " + Amount.ToString("C") + " " + Value.ToString("C"));
            }
        }
    }
}
CustosNode

namespace CompositePatternExercise
{
    abstract class CustosNode
    {
        protected string _name;
        protected decimal _amount = 0m;
        protected decimal _value = 0m;

        public decimal Value { get => _value; }
        public decimal Amount { get => _amount; }

        // Constructor
        public CustosNode(string name)
        {
            this._name = name;
        }
        public CustosNode(string name, decimal amount, decimal value)
        {
            this._name = name;
            this._amount = amount;
            this._value = value;
        }
        public abstract void Add(CustosNode c);
        public abstract void Remove(CustosNode c);
        public abstract void Display();
    }
}

查找复合设计模式。您需要将该类与父类联接,以便获得项目名称(而不是id),并且需要项目列表(不仅仅是Custo类)。Custo与
Projetos
的关系如何?每个项目都可能有许多客户吗?你有没有尝试过什么,或者只是想让别人来完成你的工作?