0

I have the following ranges in database. I am trying to check if there is an overlapping range on editing existing records.

Range looks like as follows

enter image description here

I used the following code to check overlapping ranges.

var allRanges = await _dlRangesService.GetAllRanges();
var isOverlapping = allRanges.Any(r => r.RangeStart <= rangeToUpdate.RangeStart && rangeToUpdate.RangeStart <= r.RangeEnd && rangeToUpdate.ID != r.ID) ||
                    allRanges.Any(r => r.RangeStart <= rangeToUpdate.RangeEnd && rangeToUpdate.RangeEnd <= r.RangeEnd && rangeToUpdate.ID != r.ID);
 

and I added the following condition to through validation on save

if (isOverlapping)
    ModelState.AddModelError("Range", "Range overlaps with existing Range.");

but when I edit the first range in pic above 10000 - 10000 to 10000 - 22000 it allows me to save.

I also tried the following, in this case it won't let me save changes even if there is no overlapping range in the database. but it works on creating a new record.

 var allRanges = await _dlRangesService.GetAllRanges();
 var isOverlapping = allRanges.Any(r => r.RangeStart <= rangeToUpdate.RangeStart && rangeToUpdate.RangeStart <= r.RangeEnd) ||
                     allRanges.Any(r => r.RangeStart <= rangeToUpdate.RangeEnd && rangeToUpdate.RangeEnd <= r.RangeEnd);

Any help?

kuka muk
  • 349
  • 2
  • 18
  • 1
    Does this answer your question? [What's the most efficient way to test if two ranges overlap?](https://stackoverflow.com/questions/3269434/whats-the-most-efficient-way-to-test-if-two-ranges-overlap) – Dennis_E Jan 27 '22 at 01:46
  • `r.RangeStart <= rangeToUpdate.RangeEnd && r.RangeEnd >= rangeToUpdate.RangeStart`. Picture the two ranges sliding past each other. It's when `r1.Start == r2.End` that they begin overlapping, and then at `r1.End == r2.Start` that they end overlapping. So you test for everything in between. – Jeremy Lakeman Jan 27 '22 at 02:00
  • @JeremyLakeman I added the third condition to test `r.RangeStart <= rangeToUpdate.RangeEnd && r.RangeEnd >= rangeToUpdate.RangeStart` and seems like its working do you think I need to check more? – kuka muk Jan 27 '22 at 02:24
  • Your problem was that you were only testing if the modified range had a start or end inside an existing range. You weren't handling the case where the modified range was larger than any existing range (eg start=minvalue, end = maxvalue). The test I posted is sufficient to determine if two ranges overlap, no further test is required. – Jeremy Lakeman Jan 27 '22 at 02:35
  • Thank you so much @JeremyLakeman I run a couple of test working great! – kuka muk Jan 27 '22 at 02:36

1 Answers1

2

Of the following possibilities for overlapping ranges;

1)
   [        existing range ]
          [ modified range        ]
2)
          [ existing range        ]
   [        modified range ]
3)
   [        existing range        ]
          [ modified range ]
4)
          [ existing range ]
   [        modified range        ]

Your test was not handling case 4.

Rather than testing if a range contains the start or end of another range. You need to test that both of these cases are true;

1)
        ... range 1 ]
          [ range 2 ...
2)
          [ range 1 ...
        ... range 2 ]

eg;

   r.RangeStart <= rangeToUpdate.RangeEnd 
&& r.RangeEnd   >= rangeToUpdate.RangeStart
Jeremy Lakeman
  • 9,515
  • 25
  • 29