4

Let's say I have the following collection of items and Dates:

ItemID: 1, Date: 10/10/2018 11:30 AM
ItemID: 1, Date: 10/10/2018 11:32 AM
ItemID: 1, Date: 10/10/2018 11:33 AM
ItemID: 1, Date: 10/10/2018 11:34 AM
ItemID: 1, Date: 10/10/2018 11:57 AM

ItemID: 2, Date: 10/10/2018 7:45 AM
ItemID: 2, Date: 10/10/2018 7:49 AM

ItemID: 3, Date: 10/10/2018 8:45 AM
ItemID: 3, Date: 10/10/2018 9:13 AM

I'd like to group by ItemID and Date but the dates I want them group by the ones that fall within 5 minutes of each other

In the data set above the following would be grouped

ItemID: 1, Date: 10/10/2018 11:30 AM    <-- Grouped with Item 1
ItemID: 1, Date: 10/10/2018 11:32 AM    <-- Grouped with Item 1
ItemID: 1, Date: 10/10/2018 11:33 AM    <-- Grouped with Item 1
ItemID: 1, Date: 10/10/2018 11:34 AM    <-- Grouped with Item 1


ItemID: 1, Date: 10/10/2018 11:57 AM    <-- Not Grouped


ItemID: 2, Date: 10/10/2018 7:45 AM    <-- Grouped with Item 2
ItemID: 2, Date: 10/10/2018 7:49 AM    <-- Grouped with Item 2


ItemID: 3, Date: 10/10/2018 8:45 AM    <-- Not Grouped

ItemID: 3, Date: 10/10/2018 9:13 AM    <-- Not Grouped

The ones that are grouped are the ones that fall within 5 minutes of each other.

I know how to group them by ItemID and Date in LINQ but I'm having a hard time grouping them the way I described.

What I've tried so far:

var groupedItems = from item in items
              group item by new
              {
                  item.ItemID,
                  item.Date
              } into g
              select g;
5
  • Please share what you have tried? Oct 10, 2018 at 16:01
  • @GiladGreen Updated. Sorry I'm new to LINQ I'm just learning it. Oct 10, 2018 at 16:03
  • 1
    Is this an in memory collection or does it execute in the database? Oct 10, 2018 at 16:06
  • @GiladGreen In-memory collection Oct 10, 2018 at 16:06
  • what output you needed? Oct 10, 2018 at 16:13

2 Answers 2

5

You can try something like this:

var groupedItems = items
    .GroupBy(i => i.ItemId, (k, g) => g
        .GroupBy(i => (long)new TimeSpan(i.Date.Ticks - g.Min(e => e.Date).Ticks).TotalMinutes / 5))
    .SelectMany(g => g);
5
  • I tried it with these times: 11:12, 11:13, 11:14 and 11:15, it grouped (11:12, 11:13, 11:14) together but it did not group 11:15. The IntervalKey for the first group was 212249366, the IntervalKey for 11:15 item that was not grouped is 212249367 Oct 10, 2018 at 20:31
  • @EricBergman What are the bounds then? 11:10 to 11:15 or 11:12 to 11:17?
    – arekzyla
    Oct 10, 2018 at 20:37
  • Example if we have: 11:12, 11:13, 11:14, 11:15, 11:30, 11:35 it should group the ones that fall within 5 minutes of each other, in this case we should have two groups (11:12, 11:13, 11:14, 11:15) and (11:30, 11:35), it would be safe to assume that there will only be up to 4 items that fall within the 5 minute range within a specific hour but no more Oct 10, 2018 at 20:45
  • @EricBergman I've updated my answer. Is that what you wanted?
    – arekzyla
    Oct 10, 2018 at 20:55
  • That's exactly what I wanted, thank you so much arekzyla Oct 10, 2018 at 21:13
2

My attempt at your solution:

var dates = new List<DateTime>{ ... }; // dates get initialized however your code does it...

var groups = dates.Select(d => 
    d, 
    GroupDate = new DateTime(d.Year, d.Month, d.Day, d.Hour, (d.Minute / 5) * 5, d.Second)})
    .GroupBy(g => g.GroupDate);

The math for the Minute is going to, ultimately, separate by which whole multiple of 5 the minute falls into. Another caveat is that if you don't care about day/month/year and want to group everything explicitly by minute, then scratch the new DateTime(...) piece and replace with (d.Minute / 5) * 5. That will group by minute regardless of day/month/year.

Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.