代码之家  ›  专栏  ›  技术社区  ›  Ankit

对Datetime的LINQ-to-SQL检查为空或小于当前日期

  •  0
  • Ankit  · 技术社区  · 7 年前

        Select * from Employee where Employee.StartDate is null OR 
    (Employee.StartDate is not null AND Employee.StartDate > GetDate())
    

    我试着遵循密码

        Employee.Where(e => e.StartDate == null || 
    (e.StartDate != null && e.StartDate > Datetime.Today);
    
        Employee.Where(e => e.StartDate.HasValue == false || 
    (e.StartDate.HasValue != false && e.StartDate > Datetime.Today);
    
        Employee.Where(e => e.StartDate..Equals((DateTime?)null) || 
    (e.StartDate.HasValue != false && e.StartDate > Datetime.Today);
    

    3 回复  |  直到 7 年前
        1
  •  2
  •   Gert Arnold    7 年前

    我好像不明白我在评论中的意思。几乎可以肯定的是 StartDate 根据需要映射。这可以通过数据注释来完成。。。

    [Required]
    public DateTime? StartDate { get; set; }
    

    …或通过流畅的映射,例如 OnModelCreating ...

    modelBuilder.Entity<Employee>()
        .Property(e => e.StartDate).IsRequired();
    

    当然,根据需要标记可为空的属性没有多大意义,所以您应该使其不可为空或不必为空。

        2
  •  1
  •   Harald Coppoolse    7 年前
    var thresholdDate = GetDate();
    var employeesWithNewOrNoStartDate = dbContext.Employees
        .Where(employee => employee.StartDate == null
                        || employee.StartDate > thresholdDate);
    

    换句话说:

    从所有的序列 Employees ,只拿那些 员工 没有 StartDate 起始日期

        3
  •  0
  •   Clinton Okorie    7 年前
    var NewGroupEmployees = dbContext.Employees
    .Where(employee => employee.StartDate == null
                    || employee.StartDate > DateTime.Today).ToList();
    

    这将返回一个具有所需结果的列表。

    存储过程 在SQL中,并从应用程序调用它。