I have a List of MyObject as below:
public MyObject(reqVal, reqTime)
{
_value = reqVal;
_time = reqTime;
}
public double Value
{
get {return _value;}
}
public DateTime Time
{
get {return _time;}
}
var myList = new List<MyObject>();
myList.Add(new MyObject(100, new DateTime(2012, 03, 01, 10, 0, 0));
myList.Add(new MyObject(50, new DateTime(2012, 03, 01, 10, 3, 0));
myList.Add(new MyObject(10, new DateTime(2012, 03, 01, 10, 6, 0));
myList.Add(new MyObject(230, new DateTime(2012, 03, 01, 10, 9, 0));
....
As you can see this list holds the values for a whole day and each value is generated every 3 min. How can I find the following based on the chunk of 15 min:
- maxVal
- minVal
- openVal
- closeVal
so if the 1st datetime value is 2012/03/01 10:00
I need to find the 4 above between 10:00 and 10:15 then next set of 4 between 10:15 and 10:30 and so on …
so all these values will be calculated based on their time range
for example 2nd maxVal or openVal will be the maxVal and openVal between 10:15 and 10:30
any help would be much appreciated,
Thanks.
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
TimeDatais the same asMyObject):So what is happening is:
(t.Time.Minute / 15)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.You could probably make this run faster by doing the
GroupBya bit differently (i.e. using a class as the group key instead of building a string for each one).UPDATE: I generated myself a better test case for this by adding 10,000 elements to
myListwith random values for the value and minute. I added a.ToList()on the end of the generation ofgroupedto 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 aStopWatch). Looks like performance isn’t a huge issue unless resources are constrained and you’ve got hundreds of thousands of elements inmyList.