# Print Non-Overlapping Items Based On Time

• ### Question

• Hello Guys,

Please, i have some trouble trying to print out items that don't have their end time not overlapping the next item start item. My algorithm is quite weak for now.

So, from the code below, i need to print out ID 1, 3, 4. So the idea is to have the item in sequential order based on the time.

If you look at Id 2 start item (8AM), you will see it does not continue from the end time of Id 1 (9:30AM), so i dont wana print that. But Id 3 continues from where Id 1 end time etc

``` static void Test()
{
List<Sections> sections = new List<Sections>
{
new Sections {Id = 1, Name = "Room A" , Start = new DateTime(2019, 01, 27, 8, 0, 0).ToShortTimeString(), End = new DateTime(2019, 01, 27, 9, 30, 0).ToShortTimeString() },
new Sections {Id = 2, Name = "Room B" , Start = new DateTime(2019, 01, 27, 8, 0, 0).ToShortTimeString(), End = new DateTime(2019, 01, 27, 10, 0, 0).ToShortTimeString() },
new Sections {Id = 3, Name = "Room C" , Start = new DateTime(2019, 01, 27, 9, 30, 0).ToShortTimeString(), End = new DateTime(2019, 01, 27, 11, 0, 0).ToShortTimeString() },
new Sections {Id = 4, Name = "Room D" , Start = new DateTime(2019, 01, 27, 11, 0, 0).ToShortTimeString(), End = new DateTime(2019, 01, 27, 12, 0, 0).ToShortTimeString() },
new Sections {Id = 5, Name = "Room E" , Start = new DateTime(2019, 01, 27, 13, 0, 0).ToShortTimeString(), End = new DateTime(2019, 01, 27, 14, 0, 0).ToShortTimeString()}
};
foreach (var section in sections)
{
Console.WriteLine("{0} {1}: {2} - {3}", section.Id, section.Name, section.Start, section.End);

}
}```

Any help will be appreciated to solve this.

• Edited by Tuesday, January 29, 2019 5:01 AM
Tuesday, January 29, 2019 4:30 AM

### All replies

• And this is an ADO.NET Entity Framework issue? It seems that you could post to the C# forum for help.

https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=csharpgeneral

Tuesday, January 29, 2019 6:10 AM
• And this is an ADO.NET Entity Framework issue? It seems that you could post to the C# forum for help.

https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=csharpgeneral

I was hoping i can use LINQ to solve this with some loop. Reason why i posted here.

I guess i will post that in the C# forum as well.

Tuesday, January 29, 2019 6:24 AM
• Hi Godymn,

According to your description, We could achieve it via c# loop method, the following method for your reference.

```List<Sections> sections = new List<Sections>
{
new Sections {Id = 1, Name = "Room A" , Start = new DateTime(2019, 01, 27, 8, 0, 0).ToShortTimeString(), End = new DateTime(2019, 01, 27, 9, 30, 0).ToShortTimeString() },
new Sections {Id = 2, Name = "Room B" , Start = new DateTime(2019, 01, 27, 8, 0, 0).ToShortTimeString(), End = new DateTime(2019, 01, 27, 10, 0, 0).ToShortTimeString() },
new Sections {Id = 3, Name = "Room C" , Start = new DateTime(2019, 01, 27, 9, 30, 0).ToShortTimeString(), End = new DateTime(2019, 01, 27, 11, 0, 0).ToShortTimeString() },
new Sections {Id = 4, Name = "Room D" , Start = new DateTime(2019, 01, 27, 11, 0, 0).ToShortTimeString(), End = new DateTime(2019, 01, 27, 12, 0, 0).ToShortTimeString() },
new Sections {Id = 5, Name = "Room E" , Start = new DateTime(2019, 01, 27, 13, 0, 0).ToShortTimeString(), End = new DateTime(2019, 01, 27, 14, 0, 0).ToShortTimeString()}
};

Sections tmpSection = new Sections();

List<Sections> list = new List<Sections>();

for (int i = 0; i < sections.Count() -1; i++)
{
for (int j = sections.Count() - 1; j > i; j--)
{
if (sections[i].End == sections[j].Start)
{
if (!list.Contains(sections[i]))
{
}
if (!list.Contains(sections[j]))
{
}
}
}
}

foreach (var section in list)
{

Console.WriteLine("{0} {1}: {2} - {3}", section.Id, section.Name, section.Start, section.End);

}```

Best regards,

Zhanglong

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

Wednesday, January 30, 2019 1:46 AM