Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could try to group the list by which 15-minute interval it's in. Here's a somewhat quick-and-dirty example (I've changed some of your values to present better test cases, and <code>TimeData</code> is the same as <code>MyObject</code>):</p> <pre><code>List&lt;TimeData&gt; myList = new List&lt;TimeData&gt;(); myList.Add(new TimeData(100, new DateTime(2012, 03, 01, 10, 0, 0))); myList.Add(new TimeData(50, new DateTime(2012, 03, 01, 10, 3, 0))); myList.Add(new TimeData(10, new DateTime(2012, 03, 01, 10, 35, 0))); myList.Add(new TimeData(230, new DateTime(2012, 03, 01, 10, 46, 0))); var grouped = myList.GroupBy(t =&gt; t.Time.Day.ToString() + "_" + t.Time.Month.ToString() + "_" + t.Time.Year.ToString() + "_" + t.Time.Hour.ToString() + "_" + (t.Time.Minute / 15).ToString()) .Select(gr =&gt; new { TimeSlot = gr.Key, Max = gr.Max(item =&gt; item.Value), Min = gr.Min(item =&gt; item.Value), Open = gr.OrderBy(g =&gt; g.Time).First().Value, Close = gr.OrderBy(g =&gt; g.Time).Last().Value }); </code></pre> <p>So what is happening is:</p> <ul> <li>Group by the day + month + year + hour + (minute / 15). <ul> <li>The <code>(t.Time.Minute / 15)</code> is integer division, which means that any minute value from 0 to 14 will equate to 0, 15 to 29 will be 1, and so on.</li> </ul></li> <li>Convert each group into a new class (I've made it anonymous for space's sake), with each group's key (the string generated in the GroupBy) and the max, min, first and last values in the group.</li> </ul> <p>You could probably make this run faster by doing the <code>GroupBy</code> a bit differently (i.e. using a class as the group key instead of building a string for each one).</p> <p>UPDATE: I generated myself a better test case for this by adding 10,000 elements to <code>myList</code> with random values for the value and minute. I added a <code>.ToList()</code> on the end of the generation of <code>grouped</code> to make sure lazy evaluation wasn't a factor. It ran in 34 milliseconds. I tried with 100,000 elements and it ran in 226 milliseconds (as measured by a <code>StopWatch</code>). Looks like performance isn't a huge issue unless resources are constrained and you've got hundreds of thousands of elements in <code>myList</code>.</p>
 

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