【问题标题】:Asp.net MVC Booked StartDate and EndDate checkAsp.net MVC Booked StartDate 和 EndDate 检查
【发布时间】:2020-09-24 15:47:27
【问题描述】:

我想检查我的第一次预订是否 res2.StartDate 和 res2.EndDate 与我的特定房间的 res1.StartDate 和 res1.EndDate 不同。 而且不在 res1.StartDate 和 res1.EndDate 之间,所以我可以预订房间。

我已经这样做了

var result = from r in _reservationData.GetAll()
             .Where(r => r.RoomId == reservation.RoomId)
             .Where(r => r.StartDate == reservation.StartDate)
             .Where(r => r.EndDate == reservation.EndDate)
                     orderby r.StartDate
                     select r;

但这只是检查它们是否相等。如何检查reservation2期间是否不在reservation1期间?

提前谢谢你。

这是未更新:

private bool CheckIfThisRoomIsAvailable(Reservation reservation)
    {
        // return the room data
        var result3 = _roomData.GetById(reservation.RoomId);

        // return the reservations of this room and 
        // check if the startdate of the new reservation
        // is greater than the enddate of the old reservation
        var result1 = from r in _reservationData.GetAll()
            .Where(r => (r.RoomId == reservation.RoomId)
             && !(r.EndDate < reservation.StartDate))
                      select r;

        /* return the reservations of this room and check if the startdate  
         of the new reservation less than the enddate of old reservation
         and check if the enddate of the new reservation is less than 
         the startdate of the old reservation
        */
        var result2 = from r in _reservationData.GetAll()
             .Where(r => (r.RoomId == reservation.RoomId)
              && !(r.EndDate > reservation.StartDate)
              && !(r.StartDate > reservation.EndDate))
                      select r;

        // checking condtions
        if (!result3.Status.Contains("Available"))
        {
            return false;
        }

        else if (result1 is null)
        {
            return true;
        }

        else if (result2 is null)
        {
            return true;
        }

        else
        { return false; }
    }

但问题是if条件,result1或2为null,它返回false??

【问题讨论】:

  • 检查 roomid 是否相同,然后 reservation.StartDate 是否落在任何现有预订的开始/结束之间或之间 reservation.EndDate 是否落在或中间在任何现有预订的开始/结束之间。为此使用大于和小于符号。
  • 我还假设这是永远不会投入生产的代码(即家庭作业)。如果不是这种情况,您还需要了解时区如何与调度一起工作。想想来自服务器时区之外的请求,以及更新时区时会发生什么(例如,在不久的将来某个时区将取消夏令时,使用您的系统,并且有未来的预订)。
  • 是的,这不是用于生产的作业。

标签: c# asp.net-mvc linq


【解决方案1】:

我用这个条件解决了问题:

var canIBookTheRoom =
            !_reservationData.GetAll()
            .Where(r => r.RoomId == reservation.RoomId)
            .Where(r => (r.StartDate <= reservation.EndDate)
                && (r.EndDate >= reservation.StartDate)).ToList().Any();

        // checking condtions
        if (!result3.Status.Contains("Available"))
        {
            return false;
        }
        else if (canIBookTheRoom) 
        { return true; }
        else return false;

我希望这对任何人都有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多