0

I have a list of answers that i put into groups by matching the FormId then the ProductInstance and the lastly by TimeStamp like so:

var groups = answersForCustomerList.GroupBy(x => new { x.FormId, x.ProductInstance, x.TracingData.Timestamp });

which gives me groups like this:

FormId = {97748e5e-be41-4d53-9ef8-f0dc150e58b6}, ProductInstance = "kontot", Timestamp = {2016-12-20 16:00:00}
FormId = {87dd356b-2770-4427-89d2-7503ee80bb1c}, ProductInstance = "Lån", Timestamp = {2016-12-20 16:02:00} 
FormId = {87dd356b-2770-4427-89d2-7503ee80bb1c}, ProductInstance = "Lån", Timestamp = {2016-12-20 16:01:00} 
FormId = {97748e5e-be41-4d53-9ef8-f0dc150e58b6}, ProductInstance = "Konto", Timestamp = {2016-12-20 15:00:00}
FormId = {87dd356b-2770-4427-89d2-7503ee80bb1c}, ProductInstance = "Lån", Timestamp = {2016-12-20 15:59:00}

but what i need to do and cant seem to figure how is how to group timestamps that is within +/- 1 minute into the same group. So answers with the same FormId and ProductInstance but with TimeStamp 16:01:00 or 15:59:00 get in the same group as the 16:00:00 group.

Jack
  • 1
  • 2
  • 1
    16:02:00 should go to same group, or it's new group? Also which timestamp value should be in result? – Sergey Berezovskiy Mar 14 '17 at 13:28
  • 1
    So 15:58:59 belongs to a different group than 15:59:00 even if it's +-1 minute? – Tim Schmelter Mar 14 '17 at 13:30
  • Yes, 16:02:00 and 15:58:59 should go into different groups, the timestamp that should be in the result would probably be the latest one – Jack Mar 14 '17 at 13:33
  • Sorry the question might have been a bit poorly described, i have update it – Jack Mar 14 '17 at 13:39
  • You could group on the Date, Hour, and then the Minutes divided by 3. That would group minutes 00, 01, and 02 together, then 03, 04, and 05 and so on. Otherwise you need to to something more complex to determine what should be grouped. – juharr Mar 14 '17 at 13:40
  • What happens if you have three records: 15:58,15:59,16:00? – Ofir Winegarten Mar 14 '17 at 13:45
  • I hadn't really thought about how that would work, maybe i need to rethink how i approach this. Thanks for all the answers! – Jack Mar 14 '17 at 13:53

1 Answers1

0

I managed to sort-of get what i wanted with this:

 var groups = answersForCustomer
            .GroupBy(x =>
            {
                var stamp = x.TracingData.Timestamp;
                stamp = stamp.AddMinutes(-(stamp.Minute % 2));
                stamp = stamp.AddMilliseconds(-stamp.Millisecond - 1000 * stamp.Second);
                return new { stamp, x.FormId, x.ProductInstance };
            });

Which gives me groups with 2 minutes forward in time, thanks for all the answers!

Jack
  • 1
  • 2