简体   繁体   English

从数据库中获取介于两个日期之间的数据

[英]get data from database that falls in between 2 dates

I have a data base which has various columns among which are the columns "Effective Date" and "Termination Date" 我有一个数据库,其中包含各列,其中包括“生效日期”和“终止日期”列

The situation is before adding a record I need to make sure that there is no overlapping record for the given set of Effective and Termination dates. 情况是在添加记录之前我需要确保给定的有效和终止日期集合没有重叠记录。 How could I implement this? 我怎么能实现这个? I am building the app with MVC using LINQ. 我正在使用LINQ使用MVC构建应用程序。

Thanks! 谢谢!

You should check any of records startDate and TerminationDate are in your new record's start and termination date or not, you can do this with linq: 您应该检查startDate和TerminationDate中的任何记录是否在新记录的开始和终止日期,您可以使用linq执行此操作:

bool overlap = db.Records.Any(x=>(x.TerminationDate >= givenRecord.StartDate 
                  && x.StartDate <= givenRecord.TerminationDate) ||
                  (x.TerminationDate <= givenRecord.TerminationDate 
                  && x.StartDate >= givenRecord.StartDate));
var result = (from t in dbContext.table
             where t.EffectiveDate.Value <= yourDate && 
             t.TerminationDate.Value >= youDate
             select t).Count();
if(result > 0)
{
//Overlap
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM