Created
September 10, 2024 17:29
-
-
Save leandromoh/4cd3ad29c0bdb69265314553ee01d6bf to your computer and use it in GitHub Desktop.
group values by time.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
var series = new List<Sample>(); | |
series.Add(new (){ timestamp = DateTime.Now.AddMinutes(-3) }); | |
series.Add(new (){ timestamp = DateTime.Now.AddMinutes(0) }); | |
series.Add(new (){ timestamp = DateTime.Now.AddMinutes(3) }); | |
series.Add(new (){ timestamp = DateTime.Now.AddMinutes(4) }); | |
series.Add(new (){ timestamp = DateTime.Now.AddMinutes(5) }); | |
series.Add(new (){ timestamp = DateTime.Now.AddMinutes(6) }); | |
series.Add(new (){ timestamp = DateTime.Now.AddMinutes(7) }); | |
series.Add(new (){ timestamp = DateTime.Now.AddMinutes(15) }); | |
var groups = series.GroupBy(x => | |
{ | |
var ticks = TimeSpan.FromMinutes(5).Ticks; | |
var stamp = x.timestamp.Ticks % ticks; | |
return x.timestamp.AddTicks(-stamp + ticks); | |
}) | |
.ToList(); | |
foreach(var g in groups) | |
{ | |
Console.WriteLine(g.Key); | |
foreach(var x in g) | |
{ | |
Console.WriteLine(x); | |
} | |
Console.WriteLine(); | |
} | |
public record class Sample | |
{ | |
public DateTime timestamp; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output