如何使用C#Lambda表达式确定可用的时隙?

时间:2016-08-25 05:02:26

标签: c# lambda

假设有一个表名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

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

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";
    }

2 个答案:

答案 0 :(得分:0)

希望能帮到你

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";
    }

答案 1 :(得分:0)

让我们尝试下面的代码,希望它可以帮助你:

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";
}