LINQ 查询在应该为 true 时返回 false

本文关键字:true 返回 false 查询 LINQ | 更新日期: 2023-09-27 18:37:08

我有以下由前开发人员编写的 LINQ 查询,它在应该工作的时候不起作用。

        public bool IsAvailable(Appointment appointment)
    {
        var appointments = _appointmentRepository.Get;
        var shifts = _scheduleRepository.Get;
        var city = _customerRepository.Find(appointment.CustomerId).City ?? appointment.Customer.City;
        const int durationHour = 1;
        DateTime scheduledEndDate = appointment.ScheduledTime.Add(new TimeSpan(durationHour, 0, 0));
        var inWorkingHours = shifts
            .Where(x =>
                //Check if any available working hours
                x.Employee.City == city &&
                x.ShiftStart <= appointment.ScheduledTime &&
                x.ShiftEnd >= scheduledEndDate &&
                //check if not booked yet
                !appointments
                    .Where(a =>
                        (appointment.Id == 0 || a.Id != appointment.Id) &&
                        a.Employee.Id == x.Employee.Id &&
                        (
                            (a.ScheduledTime <= appointment.ScheduledTime &&
                             appointment.ScheduledTime <= EntityFunctions.AddHours(a.ScheduledTime, durationHour)) ||
                            (a.ScheduledTime <= scheduledEndDate &&
                             scheduledEndDate <= EntityFunctions.AddHours(a.ScheduledTime, durationHour))
                            ))
                    .Select(a => a.Employee.Id)
                    .Contains(x.Employee.Id)
            );
        if (inWorkingHours.Any())
        {
            var assignedEmployee = inWorkingHours.FirstOrDefault().Employee;
            appointment.EmployeeId = assignedEmployee.Id;
            appointment.Employee = assignedEmployee;
            return true;
        }
        return false;
    }

    public class Appointment
{
    [Key]
    public int Id { get; set; }
    public int CustomerId { get; set; }
    public virtual Customer Customer { get; set; }
    public DateTime ScheduledTime { get; set; }
    public int? EmployeeId { get; set; }
    public virtual Employee Employee { get; set; }
}
public class Customer
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string Province { get; set; }
    public string PostalCode { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }
    public virtual string Fullname { get { return FirstName + " " + LastName; } }
    public Customer(){ }
}
public class Employee
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string Province { get; set; }
    public string PostalCode { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }
    public virtual string Fullname { get { return FirstName + " " + LastName; } }
    public Employee() { }
}
public class Shift
{
    [Key]
    public int Id { get; set; }
    public DateTime ShiftStart { get; set; }
    public DateTime ShiftEnd { get; set; }
    public int EmployeeId { get; set; }
    public virtual Employee Employee { get; set; }
}

该查询假定处理以下方案

  1. 给定一个约会,在班次开始和班次结束时间之间安排了时间,但与同一城市的任何员工都不匹配 - (返回 true)
  2. 假设约会在班次开始和班次结束时间之间安排时间,并且该班次的员工与客户位于同一城市(返回 True 并分配给员工)

如果客户与员工不在同一城市,我们将约会分配为"未分配",只要预定时间在员工轮班开始/结束时间内

如果客户与员工位于同一城市,我们将约会分配给其中一名员工(firstOrdefault)并占用该时间段。

约会不能重叠(分配的)。未分配不能相互重叠。

这个查询用于工作(我被告知)。但现在没有了,我尝试重构它和其他各种路径,但没有运气。我现在在第二周,只是不知道查询中的问题在哪里或如何编写它。

如果我需要进一步发布任何内容,请告诉我。我已经验证了约会、班次、城市都填充了有效数据,因此问题似乎不在于空数据或缺失数据。

LINQ 查询在应该为 true 时返回 false

首先,"此查询用于工作(我被告知)"是什么意思并不是 100% 清楚。但现在没有了"。您能否提供一些案例来说明它如何"不起作用"?

从我的角度来看,查询看起来几乎是正确的,但我想有一些奇怪的事情。让我们看看这段代码:

!appointments
    .Where(a =>
        (appointment.Id == 0 || a.Id != appointment.Id) &&
        a.Employee.Id == x.Employee.Id &&
        (
            (a.ScheduledTime <= appointment.ScheduledTime &&
            appointment.ScheduledTime <= EntityFunctions.AddHours(a.ScheduledTime, durationHour)) ||
            (a.ScheduledTime <= scheduledEndDate &&
            scheduledEndDate <= EntityFunctions.AddHours(a.ScheduledTime, durationHour))
         ))
    .Select(a => a.Employee.Id)
    .Contains(x.Employee.Id)

在 WHERE 条件下,除了其他所有内容外,您还按a.Employee.Id == x.Employee.Id进行过滤。这意味着 WHERE 子句之后的集合将仅包含单个员工的任命。所以我想我们可以将这部分重写为:

!appointments.Any(a =>
    (appointment.Id == 0 || a.Id != appointment.Id) &&
    a.Employee.Id == x.Employee.Id &&
    (
        (a.ScheduledTime <= appointment.ScheduledTime &&
        appointment.ScheduledTime <= EntityFunctions.AddHours(a.ScheduledTime, durationHour)) ||
        (a.ScheduledTime <= scheduledEndDate &&
        scheduledEndDate <= EntityFunctions.AddHours(a.ScheduledTime, durationHour))
     ))

另一个可能是:开始/结束日期的比较。在上面的代码中,您将检查以下内容:

.........[----------------]...........
               ^^^^^^
               ||||||
      start date and end date 
        not in this interval

所以你检查:

  • (新约会的)开始日期不在另一个约会期间的某个位置
  • 并且(新约会的)结束日期不在另一个约会期间的某个位置

这意味着您将不会遇到以下情况:

.........[----------------]...........
     ^                           ^    
     |                           |    
 start date                  end date 

这也是不可接受的。但是根据您的代码const int durationHour = 1;.因此,每次会议持续一个小时,这种制动情况对您来说应该不是问题。

无论如何,代码制动的某种示例数据会很棒。