简体   繁体   中英

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.

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:

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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