You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

While循环问题:如何仅为每个餐次添加一次餐厅预订记录

餐厅预订重复生成问题修复方案

问题根源

  • 原有代码按小时循环遍历入住时段,每一个落在餐段范围内的小时都会新增一条预订记录,比如7-11点的早餐时段最多会生成4条重复的早餐预订。
  • 枚举比较语法错误,VB中不能用Not 变量 = 枚举值的写法,且直接调用List.Last()没有做空列表判断,容易触发空引用异常。

修复逻辑

改为按天遍历入住时段,每天仅对三个餐段各做一次存在性校验,确认无重复后再新增预订:

  1. 剥离起止日期的时间部分按天遍历
  2. 每个餐段先校验当日是否已有同类型预订
  3. 确认餐段默认时间落在实际入住时间范围内再新增

完整可运行代码

Private Sub Button_addGuest_Click(sender As Object, e As EventArgs) Handles Button_addGuest.Click
    ' 按天遍历入住时间段
    Dim currentDay As Date = Start_date.Date
    Dim endDay As Date = End_date.Date
    
    While currentDay <= endDay
        ' 处理当日早餐 默认8点开餐
        Dim hasBreakfast = Restaurant_reservations.Any(Function(r) r.预订时间属性.Date = currentDay _
                                                        AndAlso r.kind_of_meals = booking_of_restaurant.kind_of_meals.breakfast)
        If Not hasBreakfast
            Dim mealTime = New Date(currentDay.Year, currentDay.Month, currentDay.Day, 8, 0, 0)
            If mealTime >= Start_date AndAlso mealTime < End_date
                Restaurant_reservations.Add(New booking_of_restaurant(mealTime, booking_of_restaurant.kind_of_meals.breakfast, characteristic))
            End If
        End If
        
        ' 处理当日午餐 默认12点开餐
        Dim hasLunch = Restaurant_reservations.Any(Function(r) r.预订时间属性.Date = currentDay _
                                                        AndAlso r.kind_of_meals = booking_of_restaurant.kind_of_meals.lunch)
        If Not hasLunch
            Dim mealTime = New Date(currentDay.Year, currentDay.Month, currentDay.Day, 12, 0, 0)
            If mealTime >= Start_date AndAlso mealTime < End_date
                Restaurant_reservations.Add(New booking_of_restaurant(mealTime, booking_of_restaurant.kind_of_meals.lunch, characteristic))
            End If
        End If
        
        ' 处理当日晚餐 默认19点开餐
        Dim hasDinner = Restaurant_reservations.Any(Function(r) r.预订时间属性.Date = currentDay _
                                                        AndAlso r.kind_of_meals = booking_of_restaurant.kind_of_meals.dinner)
        If Not hasDinner
            Dim mealTime = New Date(currentDay.Year, currentDay.Month, currentDay.Day, 19, 0, 0)
            If mealTime >= Start_date AndAlso mealTime < End_date
                Restaurant_reservations.Add(New booking_of_restaurant(mealTime, booking_of_restaurant.kind_of_meals.dinner, characteristic))
            End If
        End If
        
        currentDay = currentDay.AddDays(1)
    End While
End Sub

注意:请将代码中的预订时间属性替换为booking_of_restaurant类中存储预订时间的实际属性名,即你构造函数中第一个参数对应的类属性。

原枚举报错修复

你之前的枚举比较语法有误,正确的判断写法如下:

' 先判断列表不为空再取最后一条元素
If Restaurant_reservations.Any() AndAlso Restaurant_reservations.Last().kind_of_meals <> booking_of_restaurant.kind_of_meals.breakfast Then
    ' 你的业务逻辑
End If

内容的提问来源于stack exchange,提问作者Daniel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 18:09:03