-2

I have meeting date say 19/03/2022 7:am to 9 am.

I want to check if above schedule date time is conflicting with list of other event which has got start and end date time similar to above format.

I want to do this in c# can anyone help me with above requirement.

public class ScheduledEvent {
  public int EventId { get; set; }
  public DateTime StartDateTime {get; set; }
  public DateTime EndDateTime { get; set; } 
}

List<ScheduledEvent> Schedules - has list of already scheduled date times.

Bool checkdatetimeconflict(datetime startdatetime, datetime enddatetime)
{
  List<PlannedEvents> lstEvents;//already planned event are in this list
}

lstEvents has list of EventId, StartDateTime,EndDateTime as attributes in the classes

MaartenDev
  • 5,631
  • 5
  • 21
  • 33
Rajesh
  • 1
  • 2

1 Answers1

0

You could use LINQ to determine if a meeting exists in the specified time range. Checkout the following example:

using System;
using System.Collections.Generic;
using System.Linq;

class PlannedEvent
{
    public int EventId { get; set; }
    public DateTime StartDateTime {get; set; }
    public DateTime EndDateTime { get; set; } 
}

class Schedule
{
    public static bool MeetingConflictsWithOtherMeeting(List<PlannedEvent> plannedEvents, DateTime startDateTime, DateTime endDateTime)
    {
        return plannedEvents.Any(e =>
        {
            var startsInOtherEvent = startDateTime >= e.StartDateTime && startDateTime <= e.EndDateTime;
            var endsInOtherEvent = endDateTime >= e.StartDateTime && endDateTime <= e.EndDateTime;

            return startsInOtherEvent || endsInOtherEvent;
        });
    }
}

Example usage:

public class Program
{
    public static void Main()
    {
        var events = new List<PlannedEvent>() {
            new PlannedEvent { EventId = 1, StartDateTime = DateTime.Parse("2022-01-01 08:00"), EndDateTime =  DateTime.Parse("2022-01-01 08:30") },
            new PlannedEvent { EventId = 1, StartDateTime = DateTime.Parse("2022-01-01 10:00"), EndDateTime =  DateTime.Parse("2022-01-01 11:00") },
        };
    
        var meetingStart = DateTime.Parse("2022-01-01 08:31");
        var meetingEnd = DateTime.Parse("2022-01-01 10:04");
        
        var hasConflict = Schedule.MeetingConflictsWithOtherMeeting(events, meetingStart, meetingEnd);
        
        Console.WriteLine("Has conflict: " + hasConflict.ToString());
    }
}
MaartenDev
  • 5,631
  • 5
  • 21
  • 33