title: How can I calculate an integral?
body: <p>How to build an "integral" function from a Linq expression that returns a series of (time|value) pairs?</p>
  
  <p>E.g. my Linq expression results in a list of (date|quantity) pairs:</p>
  
  <pre><code>var quantities = db.Stocks.Find(id).Trades.Select(
      x =&gt; new
      {
          date = x.Price.Date.Subtract(unix).TotalMilliseconds,
          value = x.Quantity
      }).ToList();
  </code></pre>
  
  <p>What would be an efficient way to get a series of (date | <strong>cumulated</strong> quantity) where each record has a total of all quantities up to that point, plus the current quantity?</p>
  
  <p><strong>EDIT1</strong> - to make it concrete: what above query returns could be</p>
  
  <ul>
  <li>0: (01.01.2012 | 10) </li>
  <li>1: (01.01.2013 | -5) </li>
  <li>2: (01.01.2014 | 7)</li>
  </ul>
  
  <p>What I'm looking for is to have the cumulated value at each discrete point of time:</p>
  
  <ul>
  <li>0: (01.01.2012 | 10) </li>
  <li>1: (01.01.2013 | 5) </li>
  <li>2: (01.01.2014 | 12)</li>
  </ul>
  
  <p><strong>EDIT2</strong> - A solution might be to assemble the result 'manually' and to chain each quantity to its predecessor's quantity:</p>
  
  <pre><code>var cumulatedQuantities = new List&lt;Tuple&lt;double, double&gt;&gt;();
  cumulatedQuantities.Add(new Tuple&lt;double, double&gt;(quantities[0].date, quantities[0].value));
  for (var i = 1; i &lt;= quantities.Count - 1; i++ )
  {
      cumulatedQuantities.Add(new Tuple&lt;double, double&gt;(quantities[i].date, quantities[i].value + cumulatedQuantities[i-1].Item2));
  }
  </code></pre>
  
  <p>Somehow, that feels pretty ugly .. but it works.</p>