Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As an alternative to <a href="https://stackoverflow.com/a/19452273/659190">Servy's answer</a>, which I find more elegant and obviously much more resuable,</p> <p>You could do something more bespoke in one sweep (after ordering.)</p> <pre><code> public class ContiguousValuePeriod&lt;TValue&gt; { private readonly DateTime start; private readonly DateTime end; private readonly TValue value; public ContiguousValuePeriod( DateTime start, DateTime end, TValue value) { this.start = start; this.end = end; this.value = value; } public DateTime Start { get { return this.start; } } public DateTime End { get { return this.start; } } public TValue Value { get { return this.value; } } } public IEnumerable&lt;ContiguousValuePeriod&lt;TValue&gt;&gt; GetContiguousValuePeriods&lt;TValue, TItem&gt;( this IEnumerable&lt;TItem&gt; source, Func&lt;TItem, DateTime&gt; dateSelector, Func&lt;TItem, TValue&gt; valueSelector) { using (var iterator = source .OrderBy(t =&gt; valueSelector(t)) .ThenBy(t =&gt; dateSelector(t)) .GetEnumerator()) { if (!iterator.MoveNext()) { yield break; } var periodValue = valueSelector(iterator.Current); var periodStart = dateSelector(iterator.Current); var periodLast = periodStart; var hasTail = false; while (iterator.MoveNext()) { var thisValue = valueSelector(iterator.Current); var thisDate = dateSelector(iterator.Current); if (!thisValue.Equals(periodValue) || thisDate.Subtract(periodLast).TotalDays &gt; 1.0) { // Period change yield return new ContiguousValuePeriod( periodStart, periodLast, periodValue); periodStart = thisDate; periodValue = thisValue; hasTail = false; } else { hasTail = true; } periodLast = thisDate; } } if (hasTail) { yield return new ContiguousValuePeriod( periodStart, periodLast, periodValue); } } </code></pre> <p>which you use like,</p> <pre><code>var result = yourList.GetContiguousValuePeriods( a =&gt; a.Date, a =&gt; a.Price); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload