[英]How to check (From Date+Time - To Date+Time) using specific key condition Entity Framework Core 3.1
When the user create a meet I need to check if this room is reserved between two datetime ( From DateTime , To DateTime ) and if it is reserved return back to the same page with display message.
当用户创建会议时,我需要检查这个房间是否在两个日期时间( From DateTime , To DateTime )之间被保留,如果它被保留返回到显示消息的同一页面。
I tried to check the datatime between two periods (from datetime - to datetime) in specific place when user tried to create a meeting, system check between (from datetime - to datetime) if there any datetime between these period, system will send message to user, tell user that you can't create meeting, sense there is meeting exist in the place, but I am stuck for two days.当用户尝试创建会议时,我尝试在特定位置检查两个时间段(从日期时间到日期时间)之间的数据时间,系统在(从日期时间到日期时间)之间检查这些时间段之间是否有任何日期时间,系统会将消息发送到用户,告诉用户您无法创建会议,感觉该地方存在会议,但我被卡住了两天。
I tried to apply these solution below, but, it's work for some cases and other cases give me failure:我尝试在下面应用这些解决方案,但是,它适用于某些情况,而其他情况使我失败:
First Solution I Applied:我应用的第一个解决方案:
if (db.Meetings.Any(x => x.POMId == meetings.POMId && x.MeetingDateFrom <= meetings.MeetingDateFrom && x.MeetingDateTo >= meetings.MeetingDateTo))
{
TempData["message"] = "This date time (From Date: " + meetings.MeetingDateFrom.ToString("dd MMMM yyyy hh:mm tt") + ", To Date: " + meetings.MeetingDateTo.ToString("dd MMMM yyyy hh:mm tt") + ") in the " + placeOfMeeting + " is already booked";
TempData["color"] = "warning";
return RedirectToAction(nameof(Create));
}
Second Solution I Applied:我应用的第二个解决方案:
if (db.Meetings.Any(x => x.POMId == meetings.POMId && x.MeetingDateFrom >= meetings.MeetingDateFrom && x.MeetingDateTo <= meetings.MeetingDateTo))
{
TempData["message"] = "This date time (From Date: " + meetings.MeetingDateFrom.ToString("dd MMMM yyyy hh:mm tt") + ", To Date: " + meetings.MeetingDateTo.ToString("dd MMMM yyyy hh:mm tt") + ") in the " + placeOfMeeting + " is already booked";
TempData["color"] = "warning";
return RedirectToAction(nameof(Create));
}
Third Solution I Applied:我应用的第三个解决方案:
if (db.Meetings.Any(x => x.POMId == meetings.POMId && ((x.MeetingDateFrom <= meetings.MeetingDateFrom && x.MeetingDateTo >= meetings.MeetingDateTo) || (x.MeetingDateFrom >= meetings.MeetingDateFrom && x.MeetingDateTo <= meetings.MeetingDateTo))))
{
TempData["message"] = "This date time (From Date: " + meetings.MeetingDateFrom.ToString("dd MMMM yyyy hh:mm tt") + ", To Date: " + meetings.MeetingDateTo.ToString("dd MMMM yyyy hh:mm tt") + ") in the " + placeOfMeeting + " is already booked";
TempData["color"] = "warning";
return RedirectToAction(nameof(Create));
}
But all these solutions go through failure when testing.但是所有这些解决方案在测试时都会失败。
The question is:问题是:
How you check between two date+time (from datetime - to datetime) using specific key (place, reserve, etc...) if exist in database table or not?
如果数据库表中存在或不存在,如何使用特定键(地点、保留等...)在两个日期+时间(从日期时间到日期时间)之间进行检查?
After applying many test cases in more than 4 days work, I find the solution for this case and applied, and it is working perfectly.
在 4 天多的工作中应用了许多测试用例后,我找到了这个案例的解决方案并应用了,它运行良好。
Solution for this case is:这种情况的解决方法是:
if (db.Meetings.Any(x => x.POMId == meetings.POMId && x.MeetingDateTo >= meetings.MeetingDateFrom && x.MeetingDateFrom <= meetings.MeetingDateTo))
{
TempData["message"] = "This date time (From Date: " + meetings.MeetingDateFrom.ToString("dd MMMM yyyy hh:mm tt") + ", To Date: " + meetings.MeetingDateTo.ToString("dd MMMM yyyy hh:mm tt") + ") in the " + placeOfMeeting + " is already booked";
TempData["color"] = "warning";
return RedirectToAction(nameof(Create));
}
Explanation:解释:
You will check if the (New From Date <= All To Date from DB) && (New To Date >= All From Date from DB)您将检查(New From Date <= All To Date from DB) && (New To Date >= All From Date from DB)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.