Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7098167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:01:16+00:00 2026-05-28T11:01:16+00:00

This code seeks groups of events that occurred together. Max 5 seconds between them.

  • 0

This code seeks groups of events that occurred together.
Max 5 seconds between them. And more then 5 seconds between groups.

Update: Group is List of DateTimes. One group contains DateTimes between which less than 5 seconds. DateTimes which occured more then 5 second placing to next group.

public static List<List<DateTime>> GetGroups(int count)
{
  var groups = new List<List<DateTime>>();
  groups.Add(new List<DateTime>());

  using (var db = new DbContainer())
  {
    foreach (var row in db.Table)
    {
      if (!groups.Last().Any() || (groups.Last().Any() && (row.Time - groups.Last().Last()).TotalSeconds <= 5))
      {
        groups.Last().Add(row.Time);
      }
      else if (groups.Count < count)
      {
        groups.Add(new List<DateTime>());
        groups.Last().Add(row.Time);
        continue;
      }

      if (groups.Count == count)
      {
        break;
      }
    }
  }

  return groups;
}

Can I implement the same algoritm in LINQ in one or two expressions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T11:01:16+00:00Added an answer on May 28, 2026 at 11:01 am

    Essentially, the only tricky part about your query that’s hard to express with standard LINQ to Objects operators is grouping items based on how close consecutive ones are to each other.

    For this alone, I would use an iterator block:

    // Needs argument-checking, but you'll need another method to do it eagerly.
    public static IEnumerable<List<T>> GroupByConsective<T>
          (this IEnumerable<T> source, Func<T, T, bool> prevNextPredicate)
    {
        var currentGroup = new List<T>();
    
        foreach (var item in source)
        {
            if (!currentGroup.Any() || prevNextPredicate(currentGroup.Last(), item))
                currentGroup.Add(item); // Append: empty group or nearby elements.
            else
            {
                // The group is done: yield it out
                // and create a fresh group with the item.
                yield return currentGroup;
                currentGroup = new List<T> { item };
            }
        }
    
       // If the group still has items once the source is fully consumed,
       // we need to yield it out.
       if(currentGroup.Any())
         yield return currentGroup;
    }
    

    For everything else (projection, capping the number of groups, materializing to a collection), standard LINQ to Objects will work fine. And so your query becomes:

    using (var db = new DbContainer())
    {
       var groups = db.Table
                      .Select(row => row.Time)
                      .GroupByConsecutive((prev, next) => next.Subtract(prev)
                                                              .TotalSeconds <= 5)
                      .Take(count)
                      .ToList();
    
      // Use groups...
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems that this part of my code is where the exception occurs: c
I have this code: <?php ini_set('display_errors', true); error_reporting(E_ALL); include ../../../wp-load.php; $groups = array(); if
I just found that, in Adobe Dreamweaver CS3, this code segment seems fine in
What is wrong with this code? It is called in groups of four, and
I have some AppleScript code that I'm executing with osascript . This is part
When the compiler sees this code: SomeClass foo; int x = foo.bar; What is
I have 2 questions on this My code always seems to hit a 401
First check out this code. I seems like it should work for me, but
when I run this code, the open and seekg and tellg operation all success.
I have problem with this code: file = tempfile.TemporaryFile(mode='wrb') file.write(base64.b64decode(data)) file.flush() os.fsync(file) # file.seek(0)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.