0
IList<ExchangeSupplyData> chkDuplicate = context.ExchangeSupplyForecast
                                                .select(i => i.RASchedulingPeriodID == schedulingPeriodId 
                                                && i.RADlHdrID == record.DealNumberId 
                                                && i.ProductClassID == record.ProductTypeId 
                                                && i.SchedulingSubregionID == record.SubRegionId)

chkDuplicate will have the forecast's start and end dates of exchange supply. When a new forecast is added with the same schedulingPeriodId, DealNumberId, ProductTypeId , SubRegionId then the new start and end dates should not be in the existing forecasts start and end dates range. If the stat and end dates matches or in the range of existing records then I should return a bool to stop inserting the record.

How can I validate that in C# or LINQ? Can someone help me with this issue?

For example if we have two forecasts

First forecast's Start date: Nov 1st 2014, End date: Nov 7th 2014.

Second forecast's Start date: Nov 8st 2014, End date: Nov 17th 2014.

then If I try to add a new forecast the start date or end date should not be in the daterange Nov 1st 2014 to Nov 17th 2014

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
Murali Krishna
  • 131
  • 1
  • 2
  • 9
  • 2
    Use the [Google, Luke](http://stackoverflow.com/q/325933/1180426). Now you have to check if there's an overlap with any of the existing records. – Patryk Ćwiek Nov 06 '14 at 14:32

1 Answers1

1

You have to check if the new dates overlap with any of the already existing items.

If you want to do it with Linq, it'll be something like this:

var isAnyItemOverlapped = context.ExchangeSupplyForecast
          .Any(x => (x.StartDate >= newStartDate && x.EndDate <= newEndDate) || (x.StartDate <= newStartDate && x.EndDate >= newEndDate);

(please adapt it to your object model)

Joanvo
  • 5,677
  • 2
  • 25
  • 35