Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. how to group by specific timestamp interval like 15 min?????

how to group by specific timestamp interval like 15 min?????

Scheduled Pinned Locked Moved LINQ
csharphelptutorialquestion
12 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J J4amieC

    Yeah, thats nice and easy (when you know how). A little test code for you.

    List listTicks = new List();
    listTicks.Add(new DateTime(2009, 3, 5, 8, 0, 0));
    listTicks.Add(new DateTime(2009, 3, 5, 8, 1, 0));
    listTicks.Add(new DateTime(2009, 3, 5, 8, 7, 0));
    listTicks.Add(new DateTime(2009, 3, 5, 8, 15, 0));
    listTicks.Add(new DateTime(2009, 3, 5, 8, 17, 0));
    listTicks.Add(new DateTime(2009, 3, 5, 8, 19, 0));
    listTicks.Add(new DateTime(2009, 3, 5, 8, 22, 0));
    listTicks.Add(new DateTime(2009, 3, 5, 8, 35, 0));
    listTicks.Add(new DateTime(2009, 3, 5, 8, 37, 0));
    listTicks.Add(new DateTime(2009, 3, 5, 8, 47, 0));
    listTicks.Add(new DateTime(2009, 3, 5, 9, 15, 0));
    listTicks.Add(new DateTime(2009, 3, 5, 9, 44, 0));
    listTicks.Add(new DateTime(2009, 3, 5, 9, 59, 0));

    var result = from tick in listTicks
    group tick by new { Hour=tick.Hour ,QuarterHour=(15*Math.Floor(((double)(tick.Minute/15)))) } into g
    select new
    {
    Count = g.Count(),
    Key = g.Key,
    Items = g
    };
    foreach (var item in result)
    {
    Console.WriteLine("There are {0} items in the group starting {1:00}:{2:00}:00",
    item.Count,item.Key.Hour,item.Key.QuarterHour);
    foreach (var x in item.Items)
    Console.WriteLine("\t{0}",x );
    }

    Output is:

    There are 3 items in the group starting 08:00:00
    05/03/2009 08:00:00
    05/03/2009 08:01:00
    05/03/2009 08:07:00
    There are 4 items in the group starting 08:15:00
    05/03/2009 08:15:00
    05/03/2009 08:17:00
    05/03/2009 08:19:00
    05/03/2009 08:22:00
    There are 2 items in the group starting 08:30:00
    05/03/2009 08:35:00
    05/03/2009 08:37:00
    There are 1 items in the group starting 08:45:00
    05/03/2009 08:47:00
    There are 1 items in the group starting 09:15:00
    05/03/2009 09:15:00
    There are 1 items in the group starting 09:30:00
    05/03/2009 09:44:00
    There are 1 items in the group starting 09:45:00
    05/03/2009 09:59:00

    The crux of the matter is in the grouping in LINQ, I have group by a new anonymous class that contains the Hour and the Minute based to the previous quarter hour mark: QuarterHour=(15*Math.Floor(((double)(tick.Minute/15)))) breaking that down you get 1) tick.Minute/15 - to reduce the minute to its previous quarter 2) Cast it to a double so it can be passed to

    N Offline
    N Offline
    numpsy b
    wrote on last edited by
    #3

    If I have data from 8-22 each day and i want to separate it to 3 h he separate this way: var result = from tick in listTicks group tick by new { Hour=tick.Day ,ThreeHours=( ??? ) } into g select new { Count = g.Count(), Key = g.Key, Items = g }; normally it will seperated this way 0-2;3-5;6-8;9-11,... , but i want it 8-10;11-13;14-16;17-19;20-22 how can i this manage...?

    J 1 Reply Last reply
    0
    • N numpsy b

      If I have data from 8-22 each day and i want to separate it to 3 h he separate this way: var result = from tick in listTicks group tick by new { Hour=tick.Day ,ThreeHours=( ??? ) } into g select new { Count = g.Count(), Key = g.Key, Items = g }; normally it will seperated this way 0-2;3-5;6-8;9-11,... , but i want it 8-10;11-13;14-16;17-19;20-22 how can i this manage...?

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #4

      Im a bit exasperated you couldn't figure this out yourself with the help already given, however your problem is a simple one still. Group by the number of hours past 8 in blocks of 3 hours.

      var result = from tick in listTicks
      group tick by new { HoursPastEight = (3*Math.Floor((double)((tick.Hour-8) / 3))) } into g
      select new
      {
      Count = g.Count(),
      Key = g.Key,
      Items = g
      };

      N 1 Reply Last reply
      0
      • J J4amieC

        Im a bit exasperated you couldn't figure this out yourself with the help already given, however your problem is a simple one still. Group by the number of hours past 8 in blocks of 3 hours.

        var result = from tick in listTicks
        group tick by new { HoursPastEight = (3*Math.Floor((double)((tick.Hour-8) / 3))) } into g
        select new
        {
        Count = g.Count(),
        Key = g.Key,
        Items = g
        };

        N Offline
        N Offline
        numpsy b
        wrote on last edited by
        #5

        is it possible to have access to the last g during creating the select new { } i want to check an actual value withe one from the last g

        J 1 Reply Last reply
        0
        • N numpsy b

          is it possible to have access to the last g during creating the select new { } i want to check an actual value withe one from the last g

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #6

          Ive now provided you the very best help you're ever likely to get, and you've not even seen fit to type the words "thank you" even once, im now done helping you. You're not going to get spoon-fed in this industry, I provided you some workable examples hoping you would have a little play around yourself. Youve not. You've simply asked another mindless question. Good day sir.

          N 1 Reply Last reply
          0
          • J J4amieC

            Yeah, thats nice and easy (when you know how). A little test code for you.

            List listTicks = new List();
            listTicks.Add(new DateTime(2009, 3, 5, 8, 0, 0));
            listTicks.Add(new DateTime(2009, 3, 5, 8, 1, 0));
            listTicks.Add(new DateTime(2009, 3, 5, 8, 7, 0));
            listTicks.Add(new DateTime(2009, 3, 5, 8, 15, 0));
            listTicks.Add(new DateTime(2009, 3, 5, 8, 17, 0));
            listTicks.Add(new DateTime(2009, 3, 5, 8, 19, 0));
            listTicks.Add(new DateTime(2009, 3, 5, 8, 22, 0));
            listTicks.Add(new DateTime(2009, 3, 5, 8, 35, 0));
            listTicks.Add(new DateTime(2009, 3, 5, 8, 37, 0));
            listTicks.Add(new DateTime(2009, 3, 5, 8, 47, 0));
            listTicks.Add(new DateTime(2009, 3, 5, 9, 15, 0));
            listTicks.Add(new DateTime(2009, 3, 5, 9, 44, 0));
            listTicks.Add(new DateTime(2009, 3, 5, 9, 59, 0));

            var result = from tick in listTicks
            group tick by new { Hour=tick.Hour ,QuarterHour=(15*Math.Floor(((double)(tick.Minute/15)))) } into g
            select new
            {
            Count = g.Count(),
            Key = g.Key,
            Items = g
            };
            foreach (var item in result)
            {
            Console.WriteLine("There are {0} items in the group starting {1:00}:{2:00}:00",
            item.Count,item.Key.Hour,item.Key.QuarterHour);
            foreach (var x in item.Items)
            Console.WriteLine("\t{0}",x );
            }

            Output is:

            There are 3 items in the group starting 08:00:00
            05/03/2009 08:00:00
            05/03/2009 08:01:00
            05/03/2009 08:07:00
            There are 4 items in the group starting 08:15:00
            05/03/2009 08:15:00
            05/03/2009 08:17:00
            05/03/2009 08:19:00
            05/03/2009 08:22:00
            There are 2 items in the group starting 08:30:00
            05/03/2009 08:35:00
            05/03/2009 08:37:00
            There are 1 items in the group starting 08:45:00
            05/03/2009 08:47:00
            There are 1 items in the group starting 09:15:00
            05/03/2009 09:15:00
            There are 1 items in the group starting 09:30:00
            05/03/2009 09:44:00
            There are 1 items in the group starting 09:45:00
            05/03/2009 09:59:00

            The crux of the matter is in the grouping in LINQ, I have group by a new anonymous class that contains the Hour and the Minute based to the previous quarter hour mark: QuarterHour=(15*Math.Floor(((double)(tick.Minute/15)))) breaking that down you get 1) tick.Minute/15 - to reduce the minute to its previous quarter 2) Cast it to a double so it can be passed to

            A Offline
            A Offline
            ABitSmart
            wrote on last edited by
            #7

            Thank you :) (although I voted you 5 earlier, wanted to give it a 555) (Makes me feel guilty for not leaving the thank you note earlier)

            1 Reply Last reply
            0
            • J J4amieC

              Ive now provided you the very best help you're ever likely to get, and you've not even seen fit to type the words "thank you" even once, im now done helping you. You're not going to get spoon-fed in this industry, I provided you some workable examples hoping you would have a little play around yourself. Youve not. You've simply asked another mindless question. Good day sir.

              N Offline
              N Offline
              numpsy b
              wrote on last edited by
              #8

              hey i only can say thank you... thatsy why i pushed shortly directly on the first answer a positive reputation... i voted positive and thoughty thats what i have to do... sry that i dont said it directly... but your help was undoubtedly great! anyway do you know something about teh access?

              J 1 Reply Last reply
              0
              • N numpsy b

                hey i only can say thank you... thatsy why i pushed shortly directly on the first answer a positive reputation... i voted positive and thoughty thats what i have to do... sry that i dont said it directly... but your help was undoubtedly great! anyway do you know something about teh access?

                J Offline
                J Offline
                J4amieC
                wrote on last edited by
                #9

                Well, how about you have a try at solving your problem, and if you get stuck post the code you're up to.

                N 1 Reply Last reply
                0
                • J J4amieC

                  Well, how about you have a try at solving your problem, and if you get stuck post the code you're up to.

                  N Offline
                  N Offline
                  numpsy b
                  wrote on last edited by
                  #10
                          var result3 = (from tick in listTicks
                                         group tick by new { Day = tick.timestamp.Day, HoursPastEight = (3 \* Math.Floor(Convert.ToDouble((tick.timestamp.Hour - 8) / 3))) } into g
                                         select g).Select(g => 
                                         {
                                             // Create your object with the last timestamp
                                             var runtimeResult = new
                                             { 
                                                 Count = g.Count(),
                                                 //Key = g.Key, 
                                                 Items = g,
                                                 Timestamp = g.First().timestamp,
                                                 LastTimestamp = lastTimestamp
                                             };
                  
                                             // Set last timestamp for next iteration
                                             lastTimestamp = runtimeResult.Timestamp;
                  
                                             // Return your object
                                             return result;
                                         });
                  

                  but this wont't work because in debug i dont find Count , Items, Timestamp, LastTimestamp etc...

                  N 1 Reply Last reply
                  0
                  • N numpsy b
                            var result3 = (from tick in listTicks
                                           group tick by new { Day = tick.timestamp.Day, HoursPastEight = (3 \* Math.Floor(Convert.ToDouble((tick.timestamp.Hour - 8) / 3))) } into g
                                           select g).Select(g => 
                                           {
                                               // Create your object with the last timestamp
                                               var runtimeResult = new
                                               { 
                                                   Count = g.Count(),
                                                   //Key = g.Key, 
                                                   Items = g,
                                                   Timestamp = g.First().timestamp,
                                                   LastTimestamp = lastTimestamp
                                               };
                    
                                               // Set last timestamp for next iteration
                                               lastTimestamp = runtimeResult.Timestamp;
                    
                                               // Return your object
                                               return result;
                                           });
                    

                    but this wont't work because in debug i dont find Count , Items, Timestamp, LastTimestamp etc...

                    N Offline
                    N Offline
                    numpsy b
                    wrote on last edited by
                    #11

                    var lastTimestamp = new DateTime(1970, 1, 1);

                    is also needed... for the first round

                    N 1 Reply Last reply
                    0
                    • N numpsy b

                      var lastTimestamp = new DateTime(1970, 1, 1);

                      is also needed... for the first round

                      N Offline
                      N Offline
                      numpsy b
                      wrote on last edited by
                      #12

                      the fault is result.Timestamp -> has to be g... then it works

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups