0

Following scenario:

public class Data
{
   public Data()
   {
        Details = new List<Details>();
   }

   public List<Details> Details {get; set;}
}

public class Details
{
   public Details()
   {

   }

   public decimal A {get; set;}
   public decimal B {get, set;}
}

Later in the code:

Data dt = new Data();

Details det = new Details();
det.A = 2;    

dt.Details.Add(det); // add X amount of details to the list
.........

Now the list will look like this:

Item 1:  A = 2 , B = 0
Item 2:  A = 2 , B = 0
Item 3:  A = 2 , B = 0
Item 4:  A = 2 , B = 0
.....
Item N:  A = 2 , B = 0

How can I calculate the B property value like this:

enter image description here

Basically:

Item 1 A is the starting point and has whatever value ...

Item 1 B value will always be the same as Item 1 A

Item 2 A can be whatever

Item 2 B = Item 1 B + Item 2 A

Item 3 A can be whatever

Item 3 B = Item 2 B + Item 3 A

and so forth.

How can I iterate through the list and automatically calculate the B property value of the items?

David Dury
  • 5,537
  • 12
  • 56
  • 94
  • 3
    And? What is the problem? – Rubens Farias Mar 26 '15 at 12:44
  • How can I iterate through the list and automatically calculate the B property value of the items? – David Dury Mar 26 '15 at 12:45
  • So `B` is a running sum? – ryanyuyu Mar 26 '15 at 12:45
  • That's correct, the B value should be calculated on the fly based on the A and B value after the first item .. – David Dury Mar 26 '15 at 12:46
  • `LinkedList Class`(https://msdn.microsoft.com/en-us/library/he2s3bh7%28v=vs.110%29.aspx) ? – Rubens Farias Mar 26 '15 at 12:48
  • 1
    Are you sure you want to put that info in the `Details` object itself? It seems out of place there since it's a property that requires information that isn't available to itself. What happens if elements are moved (or removed) from the list? – ryanyuyu Mar 26 '15 at 12:49
  • You can write a method which calculates all the values of B and sets them, then you can have an event on details and data to say that it's changed and fire your method – James Mar 26 '15 at 12:50
  • for aggregation you can use [Aggregate](https://msdn.microsoft.com/en-us/library/system.linq.enumerable.aggregate(v=vs.110).aspx), but simples way simple loop – Grundy Mar 26 '15 at 12:51
  • @JamesBarrass: doesn it make any sens to use events if the data is retrieved from database? – David Dury Mar 26 '15 at 12:56
  • If it's immutable (after being read from the database) then you just calculate once after the initial query. Nothing automatic about it. If it's mutable then whenever either the data or the details change you need to re-run the calculation. – James Mar 26 '15 at 12:58
  • @ryanyuyu the elements are never moved or deleted. there will be a defined amount of items let's say 10 and that's how they will stay. nothing happens to the items themselves except the amounts that are calculated. – David Dury Mar 26 '15 at 12:58
  • So you just want a method that calculates the values of B? – James Mar 26 '15 at 12:59

2 Answers2

1

This should do it:

var prev = new Details();
foreach (var item in dt.Details)
{
    item.B = item.A + prev.B;
    prev = item;
}
Magnus
  • 45,362
  • 8
  • 80
  • 118
0

It seems you just need a running sum stored into each variable. To do this, just modify a normal running sum approach and store it into the detail.

decimal currentSum = 0;
for (int i = 0; i < detailList.Count; i++)
{
    var detail = detailList[i];
    currentSum += detail.A;
    detail.B = currentSum;
}

Since you mentioned that you are not making this calculation often (your list does not change frequently), this should more than handle the job.

ryanyuyu
  • 6,366
  • 10
  • 48
  • 53