Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Get Max Value of overlapping times in a list of Start and End Times

Avatar of SimonPrice3376
SimonPrice3376 asked on
C#LINQ Query
2 Comments1 Solution207 ViewsLast Modified:
I am trying to get a count of the max value of times where the times intersect.

The expected result that I want need from the code example below should be 4.

There are a total of 8 times, there are 6 values that intersect in total, a group of 4, and a group of 2.

What I am trying to get is the max value of intersections but just cant get it to work.

This is the code as it stands at the moment.

void Main()
{
var times = new List<Times> {
new Times
        {
            Start = DateTime.Now,
            End = DateTime.Now.AddMinutes(10)
        },
new Times
        {
            Start = DateTime.Now,
            End = DateTime.Now.AddMinutes(10)
        },
new Times
        {
            Start = DateTime.Now.AddMinutes(2),
            End = DateTime.Now.AddMinutes(10)
        },

new Times
        {
            Start = DateTime.Now.AddMinutes(15),
            End = DateTime.Now.AddMinutes(35)
        },
new Times
        {
            Start = DateTime.Now.AddMinutes(25),
            End = DateTime.Now.AddMinutes(42)
        },
new Times
        {
            Start = DateTime.Now.AddMinutes(43),
            End = DateTime.Now.AddMinutes(50)
        },
new Times
        {
            Start = DateTime.Now.AddMinutes(55),
            End = DateTime.Now.AddMinutes(89)
        },
new Times
        {
            Start = DateTime.Now.AddMinutes(2),
            End = DateTime.Now.AddMinutes(12)
        }
};


times.OrderBy(x => x.Start);

var overlappingEvents =
                        (
                        from e1 in times
                        where times
                            .Where(e2 => e1 != e2)
                            .Where(e2 => e1.Start <= e2.End)
                            .Where(e2 => e1.End >= e2.Start)
                            .Any()
                        select e1).ToList();

overlappingEvents.OrderBy(x => x.Start);
overlappingEvents.Distinct().OrderBy(x => x.Start);
overlappingEvents.Distinct().OrderBy(x => x.Start).Count();

}

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

This is the output of the times object

Start               |  End

05/04/2017 08:38:57 |  05/04/2017 08:48:57 

05/04/2017 08:38:57 |  05/04/2017 08:48:57 

05/04/2017 08:40:57 |  05/04/2017 08:48:57 

05/04/2017 08:40:57 |  05/04/2017 08:50:57 

05/04/2017 08:53:57 |  05/04/2017 09:13:57 

05/04/2017 09:03:57 |  05/04/2017 09:20:57 

05/04/2017 09:21:57 |  05/04/2017 09:28:57 

05/04/2017 09:33:57 |  05/04/2017 10:07:57 

This is the output of the overlapping object ( I have added the line where the intersect stops)

Start               |  End

05/04/2017 08:38:57 | 05/04/2017 08:48:57 

05/04/2017 08:38:57 | 05/04/2017 08:48:57 

05/04/2017 08:40:57 |  05/04/2017 08:48:57 

05/04/2017 08:40:57 |  05/04/2017 08:50:57 


---------------------------------------

05/04/2017 08:53:57 |  05/04/2017 09:13:57

05/04/2017 09:03:57 |  05/04/2017 09:20:57


I would be very grateful if someone can help me get the Max value of the intersections.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Ioannis Paraskevopoulos
Ioannis ParaskevopoulosFlag of Greece image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answers