LINQ查询应为true时返回false

devfunkd

我有以下由一位前开发人员编写的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. 在ShiftStart和ShiftEnd时间之间有一个ScheduledTime的约会,但与同一城市的任何员工都不匹配-(返回true)
  2. 给定一个在ShiftStart和ShiftEnd时间之间有ScheduledTime的约会,并且该班次的员工与客户位于同一城市(返回True,并分配给员工)

如果客户与员工不在同一城市,我们将约会分配为“未分配”,并且ScheduledTime在员工轮班开始/结束时间之内

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

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

这个查询用来工作(有人告诉我)。但是现在还不行,我已经尝试过重构它以及其他各种方法,但是都没有运气。我现在在第二周,只是不知道查询中的问题在哪里或如何编写。

让我知道是否需要发布更多内容。我已经验证了约会,轮班,城市都使用有效数据填充,因此问题似乎不存在为空或缺少数据。

IE。

首先,目前还不清楚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;因此,每次会议都持续一个小时,这种制动情况对您来说应该不是问题。

无论如何,某些示例数据会使代码崩溃。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Java matcher.matches()应为true时返回false

来自分类Dev

Isset()在应为true时返回false-Symfony PHP

来自分类Dev

查询返回true / false

来自分类Dev

VB.NET-List.Contains在应为true时始终返回false

来自分类Dev

错误-如果行中的Null值,Linq查询返回true或false

来自分类Dev

方法在应返回“ false”时返回“ true”

来自分类Dev

为什么当[[]是[]]而'{}是{}'返回False时,'()是()'返回True?

来自分类Dev

否则返回false时返回true

来自分类Dev

TSQL 转换查询以返回 TRUE/FALSE

来自分类Dev

显示 false 结果,而查询返回 true

来自分类Dev

Power Query - 合并查询返回 true 或 false

来自分类Dev

PHP PDO 类查询方法在未找到记录时返回 true 或 false

来自分类Dev

如果使用十进制字符串的`if`语句应为true,则返回false

来自分类Dev

vb函数在为false时返回true

来自分类Dev

创建对象时返回true / false

来自分类Dev

SAS模式匹配时返回true或false

来自分类Dev

intersectsBox在期望为false时返回true

来自分类Dev

NSURLSession 方法在登录时返回 true 或 false

来自分类Dev

辅助方法在应返回false时返回true

来自分类Dev

当被告知返回false时,模板返回true()

来自分类Dev

Powershell-功能匹配-返回时获得额外的true / false

来自分类Dev

当where子句的条件为false时,如何返回true?

来自分类Dev

实际上为true时,该方法返回false

来自分类Dev

如果语句应该为true时为什么返回false?

来自分类Dev

Rails 返回模型子集,a = false 总是,a = true 有时

来自分类Dev

空数组在比较时返回false,但在单独返回时返回true ...为什么这样

来自分类Dev

过程返回True或False

来自分类Dev

containsKey 返回 True False

来自分类Dev

`python3`在`list[True, True, True]`时定义一个函数,当列表中存在`False`时返回`False`

Related 相关文章

  1. 1

    Java matcher.matches()应为true时返回false

  2. 2

    Isset()在应为true时返回false-Symfony PHP

  3. 3

    查询返回true / false

  4. 4

    VB.NET-List.Contains在应为true时始终返回false

  5. 5

    错误-如果行中的Null值,Linq查询返回true或false

  6. 6

    方法在应返回“ false”时返回“ true”

  7. 7

    为什么当[[]是[]]而'{}是{}'返回False时,'()是()'返回True?

  8. 8

    否则返回false时返回true

  9. 9

    TSQL 转换查询以返回 TRUE/FALSE

  10. 10

    显示 false 结果,而查询返回 true

  11. 11

    Power Query - 合并查询返回 true 或 false

  12. 12

    PHP PDO 类查询方法在未找到记录时返回 true 或 false

  13. 13

    如果使用十进制字符串的`if`语句应为true,则返回false

  14. 14

    vb函数在为false时返回true

  15. 15

    创建对象时返回true / false

  16. 16

    SAS模式匹配时返回true或false

  17. 17

    intersectsBox在期望为false时返回true

  18. 18

    NSURLSession 方法在登录时返回 true 或 false

  19. 19

    辅助方法在应返回false时返回true

  20. 20

    当被告知返回false时,模板返回true()

  21. 21

    Powershell-功能匹配-返回时获得额外的true / false

  22. 22

    当where子句的条件为false时,如何返回true?

  23. 23

    实际上为true时,该方法返回false

  24. 24

    如果语句应该为true时为什么返回false?

  25. 25

    Rails 返回模型子集,a = false 总是,a = true 有时

  26. 26

    空数组在比较时返回false,但在单独返回时返回true ...为什么这样

  27. 27

    过程返回True或False

  28. 28

    containsKey 返回 True False

  29. 29

    `python3`在`list[True, True, True]`时定义一个函数,当列表中存在`False`时返回`False`

热门标签

归档