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 6800417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:57:59+00:00 2026-05-26T18:57:59+00:00

Assuming I have a simple structure that looks like this: public class Range {

  • 0

Assuming I have a simple structure that looks like this:

public class Range
{
    public DateTime Start { get; set; }
    public DateTime End { get; set; }

    public Range(DateTime start, DateTime end)
    {
        this.Start = start;
        this.End = end;
    }
}

And I create an collection like so:

var dr1 = new Range(new DateTime(2011, 11, 1, 12, 0, 0), 
    new DateTime(2011, 11, 1, 13, 0, 0));
var dr2 = new Range(new DateTime(2011, 11, 1, 13, 0, 0), 
    new DateTime(2011, 11, 1, 14, 0, 0));
var dr3 = new Range(new DateTime(2011, 11, 1, 14, 0, 0), 
    new DateTime(2011, 11, 1, 15, 0, 0));
var dr4 = new Range(new DateTime(2011, 11, 1, 16, 0, 0), 
    new DateTime(2011, 11, 1, 17, 0, 0));

var ranges = new List<Range>() { dr1, dr2, dr3, dr4 };

What I want to do is group the ranges where they are continuous – i.e. they are continuous if the End value of the previous Range is the same as the Start of the next.

We can assume that there are no collisions/duplicates or overlaps in the Range values.

In the example posted, I would end up with two groups:

2011-11-1 12:00:00 - 2011-11-1 15:00:00

2011-11-1 16:00:00 - 2011-11-1 17:00:00

It’s fairly easy to come up with an iterative solution for this. But is there some LINQ magic I can use to get this in a pretty one-liner?

  • 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-26T18:58:00+00:00Added an answer on May 26, 2026 at 6:58 pm

    Your best bet is to use yield and an extension method:

    static IEnumerable<Range> GroupContinuous(this IEnumerable<Range> ranges)
    {
        // Validate parameters.
    
        // Can order by start date, no overlaps, no collisions
        ranges = ranges.OrderBy(r => r.Start);
    
        // Get the enumerator.
        using (IEnumerator<Range> enumerator = ranges.GetEnumerator();
        {
            // Move to the first item, if nothing, break.
            if (!enumerator.MoveNext()) yield break;
    
            // Set the previous range.
            Range previous = enumerator.Current;
    
            // Cycle while there are more items.
            while (enumerator.MoveNext())
            {
                // Get the current item.
                Range current = enumerator.Current;
    
                // If the start date is equal to the end date
                // then merge with the previous and continue.
                if (current.Start == previous.End)
                {
                    // Merge.
                    previous = new Range(previous.Start, current.End);
    
                    // Continue.
                    continue;
                }
    
                // Yield the previous item.
                yield return previous;
    
                // The previous item is the current item.
                previous = current;
            }
    
            // Yield the previous item.
            yield return previous;
        }
    }
    

    Granted, the call to OrderBy is going to cause a full iteration of the ranges sequence, but there’s no avoiding that. Once you have it ordered, you can prevent having to materialize your results before returning them; you simply yield the results if the conditions dictate.

    If you know that the sequence is ordered, however, then you don’t have to call OrderBy at all, and you can yield items as you traverse the list and break on different collapsed Range instances.

    Ultimately, if the sequence is unordered, then you have two options:

    • Order the list and then process (remember, OrderBy is deferred as well, but will have to use one full iteration to order the sequence), using yield to return the item when you have one to process
    • Process the entire sequence at once and return as an entire materialized sequence
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a simple stored procedure that looks like this (note: this
Assuming I have a simple class that represents a staff member class Staff {
I have a group of text based rules that are structured like this: Rule
Assuming you have a simple table like the following: +---------+---------------------+ | user_id | activity_date
I have a simple POCO class that contains the student's scores. For example: Math
In building a class structure, I would like to have derived classes potentially posses
Assuming we have a simple struct like: typedef struct { int d1; int d2;
Assuming I have only the class name of a generic as a string in
Assuming I have a tree structure UL --LI ---INPUT (checkbox) And I want to
Assuming I have a class A as follows: class A{ int id; int getId(){};

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.