检查一个日期介于日期范围-Linq查询之间 [英] Check One Date is falling Between Date Range-Linq query

查看:299
本文介绍了检查一个日期介于日期范围-Linq查询之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在数据库中存储员工的请假信息.起止日期将存储在数据库中.我想阻止员工在已申请的假期范围内申请请假.

i am storing Leave Information of an employee in database.From and To Date will be stored in DB.I want to block employee to apply leave,when it comes in already applied leaves range.

例如:假设一名雇员在2015年1月1日至2015年5月1日之间已经申请请假,

Eg: Assume one Employee already Applied Leave between 01/01/2015 to 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查询找到它

How can i find it using a Linq query

推荐答案

尝试以下方法:

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");
}

这篇关于检查一个日期介于日期范围-Linq查询之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆