如何使用c# Lambda表达式确定一个时隙可用与否

本文关键字:时隙 一个 Lambda 何使用 表达式 | 更新日期: 2023-09-27 18:03:48

假设有一个表名为ClassRoutineTable:

RoomId     DayId     StartTime      EndTime
 1          1        08.00.00       10.00.00
 1          1        12.00.00       14.00.00
 2          1        10.00.00       12.00.00

现在,我需要检查特定房间和日期的时间段是否可用。例如,如果我为房间和日id 1输入开始时间或结束时间08.00.00或10.00.00,或在这两个时间之间(即开始时间或结束时间09.00.00),那将返回一条消息,即时间段不可用,否则它将保存。例如,我尝试这样做:

public string SaveRoom(Allocation allocation)
    {
        List<Allocation> checckAllocations =
            roomAllocationGateway.GetAllAllocations()
                .Where(
                    x =>
                        x.RoomId == allocation.RoomId && x.DayId == allocation.DayId && "*Code for Check time slot  *"
                       )
                .ToList();
        if (checckAllocations.Count >0)
        {
             return "Time Slot Not Available";
        }
        roomAllocationGateway.SaveRoom(allocation);
            return "saved";
    }

如何使用c# Lambda表达式确定一个时隙可用与否

希望对你有所帮助

public string SaveRoom(Allocation allocation)
    {
        List<Allocation> checckAllocations =
            roomAllocationGateway.GetAllAllocations()
                .Where(
                    x =>
                        x.RoomId == allocation.RoomId && x.DayId == allocation.DayId && x.StartTime<=allocation.EndTime &&  allocation.StartTime<=x.EndTime
                       )
                .ToList();
        if (checckAllocations.Count >0)
        {
             return "Time Slot Not Available";
        }
        roomAllocationGateway.SaveRoom(allocation);
            return "saved";
    }

让我们试试下面的代码,希望对你有所帮助:

public string SaveRoom(Allocation allocation) {
    //allocation.StartTime=08.00.00 
    //allocation.EndTime =10.00.00 
    List < Allocation > checckAllocations =
        roomAllocationGateway.GetAllAllocations()
        .Where(
            x = >
            x.RoomId == allocation.RoomId && x.DayId == allocation.DayId && ((x.startTime <= allocation.StartTime && x.startTime <= allocation.EndTime) || (x.endTime >= allocation.StartTime && x.endTime >= allocation.EndTime))
    )
        .ToList();
    if (checckAllocations.Count > 0) {
        return "Time Slot Not Available";
    }
    roomAllocationGateway.SaveRoom(allocation);
    return "saved";
}