루프의 계층 적 목록에서 하위 항목의 값을 합계하는 방법.루프의 계층 적 목록에서 하위 항목의 값을 합하는 방법
목록의 루프 중에 amount 및 price 속성 값에 하위 속성의 합계가 있어야합니다.
다음은 내 문제를 해결하는 데 사용되는 두 가지 클래스입니다. 목록의 요소를 가정
namespace SGP.Dto.Custo
{
public class PlanilhaCusto
{
public int id{ get; set; }
public int parenteId{ get; set; }
public string name { get; set; }
public decimal amount{ get; set; }
public decimal price{ get; set; }
public PlanilhaCusto(int pId, int pParenteId, pName, decimal pAmount, decimal pPrice)
{
id = pId;
parentId = pParentId;
name = pName;
amount = pAmount;
price = pPrice;
}
}
}
namespace SGP.Dto.Custo
{
public class ShowList
{
List<Dto.Custo.PlanilhaCusto> myList = new List<PlanilhaCusto>();
public void Show()
{
myList.Add(new PlanilhaCusto(1, null, "Projetos", 0, 0));
myList.Add(new PlanilhaCusto(2, 1, "Arquitetura", 5,10));
myList.Add(new PlanilhaCusto(3, 1, "Estrutura", 0, 0));
myList.Add(new PlanilhaCusto(4, 3, "Civil", 1, 50));
myList.Add(new PlanilhaCusto(5, 3, "Infra", 3, 75));
myList.Add(new PlanilhaCusto(6, null, "Pessoal", 0, 0));
myList.Add(new PlanilhaCusto(7, 6, "Mão de Obra", 20, 5700));
/*In this loop the value of the parent items must be updated
(calculated). The hierarchy of the list can be unlimited,
like a tree. I tried using a recursive method but I could
not do it.*/
foreach (var itemList in myList)
{
}
}
}
}
"재귀 적 방법을 사용했지만 시도 할 수 없었습니다."게시물을 올리십시오. * 시도한 것을 * 시도하십시오. 최악의 경우 여기 누군가가 똑같은 해결책을 씁니다. 왜냐하면 우리는 당신이 이미 시도한 것을 알지 못하기 때문입니다. 그러나 이것이 당신을 도울 것이라고 의심합니다. 그렇지 않습니까? – HimBromBeere
시도해 보셨습니까? – Alejandro
시험이나 저주 등의 질문입니까? – KinSlayerUY