I went through few answers and few match the solution I already have.
In my job scheduling app I want to check if a new added task overlaps with a task that is already in the db. The solution below gives me results for the basic scenarios but does not quite work for few. Like in case where endtime for a new task is the same as starttime of another.
e.g:
//this returns false as it should - no overlaps
New Task : 14:00 - 15:00
Existing Task : 16:00 - 17:00
//this returns true, as existing taskstart is < new takend - 17:00 overlap
New Task : 14:00 - 17:00
Existing Task : 16:00 - 17:00
//this scenario should return false
/*udpated*/the tasks do not overlap here but touch each other
New Task : 14:00 - 16:00
Existing Task : 16:00 - 17:00
private bool CheckForOtherTasks(int UserId, DateTime _dtTaskStart, DateTime _dtTaskEnd)
{
bool _bTasksExist = false;
using (var _dbContext = new task_Entities())
{
_bTasksExist = (from t in _dbContext.Tasks
where t.UserId == UserId
where t.dtTaskStart < _dtTaskEnd && _dtTaskStart < t.dtTaskEnd
select t).Any();
}
return _bTasksExist;
}
The above solution works fine until there new endtime == existing starttime or vice versa.
This solution seems to solve it but I am unsure how to implement it along with LINQ without iterating through all the records. How check intersection of DateTime periods
Would using LINQ for this task be a good approach or using a Stored Procedure to returns results be a better option?
Also, would a library such as https://www.codeproject.com/Articles/168662/Time-Period-Library-for-NET be ideal for a small project.
Cheers