title: Find start and stop in a time series
body: <p>I would like to understand if there is a way to find, using a linq exp, all start and stop windows (delimited by a start and end values) of a given time series.
  I know that this can be achieved via a normal loop but I like expanding my knowledge if possible.</p>
  
  <p>Data is stored in a ordered list of "TagData" (<code>List&lt;TagData&gt;</code>)</p>
  
  <pre><code>class TagData
  {
      public DateTime Timestamp { get; set; }
      public string Tag { get; set; }
      public double Value { get; set; }
  }
  </code></pre>
  
  <p>The easy way it is when the contents of the data are perfectly alternated such as:</p>
  
  <pre><code>timestamp               | tag       | value
  2018-12-01 00:10:00.000 | extrg_01  | 1
  2018-12-01 00:15:02.000 | extrg_01  | 0
  2018-12-01 00:25:50.000 | extrg_01  | 1
  2018-12-01 00:45:11.000 | extrg_01  | 0
  </code></pre>
  
  <p>At this point given the initial value = 1 and end value = 0, the result will be something along these lines:</p>
  
  <pre><code>timestamp_start         |timestamp_end              | tag       | nrOfSeconds
  2018-12-01 00:10:00.000 |2018-12-01 00:15:02.000    | extrg_01  | 302
  2018-12-01 00:25:50.000 |2018-12-01 00:45:01.000    | extrg_01  | 1161
  </code></pre>
  
  <p>But there also could be some "dirty" data that doesn't need to be accounted for:</p>
  
  <pre><code>timestamp               | tag       | value
  2018-12-01 00:10:00.000 | extrg_01  | 1
  2018-12-01 00:12:02.000 | extrg_01  | 1
  2018-12-01 00:15:02.000 | extrg_01  | 0
  2018-12-01 00:16:01.000 | extrg_01  | 0
  2018-12-01 00:25:50.000 | extrg_01  | 1
  2018-12-01 00:45:11.000 | extrg_01  | 0
  </code></pre>
  
  <p>In this case the end result should not differ from the first example because all values after the defined initial value (in this case was = 1) are not to be considered and likewise, only the first end value (in this case was = 0) will be considered for the calculation.</p>
  
  <p>I forgot to add the linq expression I'm trying to edit in order to achieve the result:
  Unfortunately I cannot understand how to add where conditions within this .Zip (if it is possible at all) to look for a specific value and respect a time condition to always find the next available value.       </p>
  
  <pre><code>var diffs = tagDataList.Skip(1)
              .Zip(tagDataList,
                  (curr, prev) =&gt; new
                  {
                      CurrTag = curr.Tag,
                      CurrValue = curr.Value,
                      CurrDate = curr.Timestamp,
                      PrevDate = prev.Timestamp,
                      DiffToPrev = Math.Abs((curr.Timestamp - prev.Timestamp).TotalSeconds)
                  })
              .ToList();
  </code></pre>