views:

1220

answers:

4

I have a list with tick objects including a pair of a double value and a timestamp.

I want to separate the list into child-lists depending on a time interval (for example 15 minutes). One list only has the tick objects from: 8:00:00 - 8:14-59 usw usw

C#-code:

var result = from tick in listTicks
group tick by tick.timestamp.Hour;

The datetime functions like day, hour, minute work fine but a specific interval wasn't possible for me.

It is LINQ to objects. I have already got the list from the DB and I want to transform the list.

If I have data from 8-22 each day separate to 3 h he separate this way:

0-2;3-5;6-8;9-11

With code:

var result = from tick in listTicks group tick by
(tick.timestamp.Day * 10 + tick.timestamp.Hour / 3);

I want:

8-10;11-13;14-16;17-19;20-22

I want it to separate it this way.

+1  A: 
var result = from tick in listTicks
group tick by (tick.timestamp - DateTime.MinValue).TotalMinutes / 15 into f
from c in f
select new { StartTime = DateTime.MinValue + TimeSpan.FromMinutes(c.Key),
             Items = ... };

Fixed: DateTime.Now will cause issues :)

Bug notice: Wont work with LINQ2SQL

Fix: Left to reader :)

leppie
I still feel a bit unsure about this code, as it wont work with Linq2SQL.
leppie
if i have data from 8-22 each day separate to 3 h... he separate this way 0-2;3-5;6-8;9-11 ... with codevar result = from tick in listTicks group tick by (tick.timestamp.Day * 10 + tick.timestamp.Hour / 3);but want 8-10;11-13;14-16;17-19;20-22but i want that it separates it this way ->
A: 

this work.. but why?

and how if i want to make 3 hours eg?

For 3 hours, just do 60 x 3 = 180 minutes, or use the TotalHours property and divide by 3. As for you other question, open another.
leppie
This is not an answer. Leave a comment or edit your question.
Geoffrey Chetwood
A: 

group tick by (tick.timestamp - DateTime.MinValue).TotalMinutes / 15; gives me only the ticks -> doesn't work :-(

This is not an answer. Leave a comment or edit your question.
Geoffrey Chetwood
A: 

Create a computed column in your SQL database, set this expression to the date and make it return the number of seconds represented by that date.

You can then group by the divided value, it should work fine. If you divide by 15 * 60, you'll get groups of 15 mins.

Edit:

var result = from tick in listTicks 
             group tick by tick.timestamp / (3 * 3600);

For the above to work, timestamp should be the number of seconds since some epoch, if you're using SQL sever this would be SqlDateTime.MinValue.

John Leidegren
but i already have the list in .net -> i don't want to change the db system?!
Well, you either have the value in the list so that you can query it in a manner LINQ will be able to translate or you'll have to provide that yourself.
John Leidegren
You don't need to adjust the date for every row, it's enough to adjust the epoch in the very end. All you need to know is what unit the tick.timestamp property has. Then you use integer division and divide accordingly to get what ever time slice you want. The result will be based of the same epoch.
John Leidegren
ok i understand - same like if datetime obj...group tick by ((int)(tick.timestamp - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds) / (3 * 3600)but still the problem,if i have data from 8-22 each day separate to 3 h he separate this way: 0-2;3-5;6-8;9-11, but i want 8-10;11-13;...;20-22