Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to add custom validation where if once date adds then same or between date cannot be added.

start date and End date

if suppose I add 01-06-2018 to 10-06-2018 this record

then no more between this record can be added.

suppose user enter now 06-06-2018 to 15-06-2018 then return false. because 06-06-2018 is already registered first.

What I have tried:

List lstOffers = db.OfferMasters.Where(x => ((x.OfferStartDate = dtStart) || (x.OfferStartDate >= dtEndDate && x.OfferEndDate = dtStart && x.OfferEndDate <= dtEndDate)) && x.IsActive && x.RestaurantId == RestaurantId).ToList();
Posted
Updated 28-Jun-18 6:47am
Comments
Krunal Rohit 28-Jun-18 2:07am    
Shouldn't be it like this? -

List lstOffers = db.OfferMasters.Where(x => ((x.OfferStartDate == dtStart) || (x.OfferStartDate >= dtEndDate && x.OfferEndDate == dtStart && x.OfferEndDate <= dtEndDate)) && x.IsActive && x.RestaurantId == RestaurantId).ToList();


StartDate and EndData should be compared.


KR

First of all, i'd suggest to read this: Time Period Library for .NET[^], because it may help you in understanding the relationship between two time periods...

According to your requirement, your StartDate have to be greater than Max(OfferEndDate). In other words, if StartDate is less than Max(OfferEndDate) your method should return false.
 
Share this answer
 
Assuming you want to find out whether there are any offers which overlap the specified date range:
C#
bool anyOverlappingOffers = db.OfferMasters
    .Where(x => x.RestaurantId == RestaurantId && x.IsActive)
    .Any(x => x.OfferStartDate <= dtEndDate && dtStartDate <= x.OfferEndDate);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900