简体   繁体   English

如何使用特定的关键条件 Entity Framework Core 3.1 检查(从日期+时间到日期+时间)

[英]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 DateTimeTo 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.

相关问题 如何使用实体框架组日期而不是带有时间和总和条件的日期 - How to use Entity Framework group date not date with time and sum total where condition 实体框架“从字符串中选择日期时间” - Entity framework “Select date time from string” Entity Framework 4.0/ 如何从日期和时间连接字符串 - Entity Framework 4.0/ How to concatenate string from date and time 如何使用实体框架按日期分组而不是日期与时间 - how to use entity framework to group by date not date with time 使用实体框架仅按日期对行进行计数,而无需时间 - Count row only by date without time using Entity Framework 在 ASP.NET Core 3.1 中,如何在未来的特定日期和时间安排带有托管服务的后台任务(Cron 作业)? - In ASP.NET Core 3.1, how can I schedule a background task (Cron Jobs) with hosted services for a specific date and time in the future? 如何解决Entity Framework日期时间类型错误? - How to work around Entity Framework date time type bug? 如何将日期参数和现在的时间添加到实体框架? - How to add date parameter and time now to Entity Framework? 按日期分组的实体框架总和时间 - Entity Framework sum time grouped by a date 如何使用Entity Framework将当前日期/时间插入datetime数据类型字段? - How can I insert the current date/time into a datetime data type field using Entity Framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM