【问题标题】:Check One Date is falling Between Date Range-Linq query检查一个日期是否落在日期范围-Linq 查询之间
【发布时间】:2015-01-30 08:57:27
【问题描述】:

我将员工的休假信息存储在数据库中。起止日期将存储在数据库中。我想阻止员工申请休假,当它进入已申请的休假范围时。

例如:假设一名员工已在 01/01/2015 至 05/01/2015 之间申请休假,

i)if user again try to apply leave for 04/01/2015 to 07/01/2015,then i  need to block that one.
ii)if user again try to apply leave for 05/01/2015 to 05/01/2015,then i  need to block that one.

如何使用 Linq 查询找到它

【问题讨论】:

  • 您找到有用的答案了吗?你能接受最好的吗?

标签: .net sql-server linq entity-framework lambda


【解决方案1】:

你可以尝试如下

    return (from t1 in db.Employee where ( empid == t1.EmplyeeId &&
date1 >= t1.LeaveStart && date2 <= t1.LeaveEnd))

你可以尝试如下,详细解答

int cntleaves = (from values in dbcontext.User_master
                       where values.iUser_id == Userid &&
                               ((values.dtStart_date >= Startdate &&
                                values.dtEnd_date <= Enddate)
                                 ||
                                 (values.dtStart_date <= Startdate &&
                                values.dtEnd_date <= Enddate))
                       select values).ToList().Count();

        if (cntleaves > 0) {
            ViewBag.Message = "You have already applied for leaves !";            
        }

【讨论】:

  • @vmb 我编辑了我的答案,并为您添加了另一个详细的 LINQ 查询,请执行相同的操作,如果有任何问题,请告诉我
  • 最好使用.Any()而不是.ToList().Count()
【解决方案2】:

试试这个:

var startDate = new DateTime(2015, 01, 04);
var endDate = new DateTime(2015, 01, 07);
if (_context.AppliedLeaves.Any(x => 
     x.UserId == userId &&
     ((startDate >= x.StartDate && startDate <= x.EndDate) || 
      (endDate >= x.StartDate && endDate <= x.EndDate)))
{
    throw Exception("You cannot apply this period");
}

【讨论】:

    【解决方案3】:

    另一种更好的方法是为 DateTime 类创建一个扩展方法:

    public static bool Between(this DateTime input, DateTime date1, DateTime date2)
    {
        return (input > date1 && input < date2);
    }
    

    然后你可以将它与 linq 谓词 where 子句一起使用。

    if (leaves.Where(l => l.empId == empId && dateApplied.Between(l.from, l.to)).Any()) { ... 错误... }

    PS:这只是你的方向的伪代码。

    【讨论】:

      【解决方案4】:
      IF EXISTS (
      SELECT * FROM table WHERE user = @user AND (
      @newStartDate BETWEEN appliedStartDate AND appliedEndDate
      
      )
      PRINT 'Can not apply'
      ELSE PRINT 'Can apply'
      

      【讨论】:

        【解决方案5】:

        这篇文章与范围内两列之间的查询有关,可能对一些开发人员有所帮助。

        第一步:进入列表

        var Price = GetLoad().ToList();

        第二步:你的搜索项目

        字符串代码 = "PLUS";

        var Total_Squares = 101: //值需要在整数范围内检查

        DateTime Checkdate = Convert.ToDateTime("2012-10-03"); //值需要在日期内检查

        查询1检查整数范围和字符串值

        var filtersCodeWithSquare = Price.Where(C => C.Code.StartsWith(code))

        .FirstOrDefault(r => r.FromSquare = Total_Squares); 一种 Query2检查日期范围

        var daterange = Price.FirstOrDefault(d => d.FromIntall

        = Checkdate);

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-12-27
          • 1970-01-01
          相关资源
          最近更新 更多