0

I know that something like this has been asked a hundred times, but their answers aren't working for me. Here's what I have.

I have an object (Call it ObjectA) with a start DateTime and an end DateTime. I have another collection of objects (List(ObjectB)) (different than ObjectA) that have DateTime ranges with a start and end. I am looking for all objects from the List(ObjectB) whose date range overlap the range of ObjectA. Let's see what I can do about an example...

ObjectA.StartTime = '7/31/2015 00:00:00'
ObjectA.EndTime = '8/2/2015 00:00:00'

List(ObjectB)
B1.StartTime = '7/1/2015 00:00:00'
B1.EndTime = '8/1/2015 00:00:00'

B2.StartTime = '7/1/2015 00:00:00'
B2.EndTime = '7/1/2016 23:59:59'

B3.StartTime = '8/1/2015 13:00:00'
B3.EndTime = '8/15/2015 23:59:59'

Now, my list SHOULD include B2 and B3 since both of them overlap my ObjectA times. However, it should NOT include B1 because B1's time ends prior to the other time starting.

My current code is as follows...

myList = myList.Where(x => x.StartDate < ObjectA.StartDate && x.EndDate > ObjectA.EndDate).ToList();

I am getting all 3 ObjectBs returned in the list. I have changed this a hundred different ways and I am not getting what I expect.

This is driving me nuts. Any help would be appreciated!

JamminJimE
  • 33
  • 6
  • 1
    http://stackoverflow.com/questions/13513932/algorithm-to-detect-overlapping-periods – ken2k Sep 08 '15 at 12:52
  • 1
    "I know that something like this has been asked a hundred times, but their answers aren't working for me." - then list the other questions and their answer by linking to them and explaining what does not work for you, please. Otherwise, I predict you'll get many suggestions to look at certain other questions (which you may already have read!) because we do not see how they are not fitting in your case. – O. R. Mapper Sep 08 '15 at 12:53
  • You need only to play with ObjectA's EndDate, i.e. foreach object in list of ObjectB, its StartDate should be less than EndDate of ObjectA, and also EndDate of ObjectB > EndDate of ObjecA. this statement will give you B2,B3. NOT B1. myList = myList.Where(x => x.StartDate < ObjectA.EndDate && x.EndDate > ObjectA.EndDate).ToList(); – M_Idrees Sep 08 '15 at 13:25
  • @MuhammadIdrees That solution will only catch the ObjectB's that begin before or during ObjectA and ends after it. It would fail on ObjectB's that take place at the same time as ObjectA but ends before it. – Andreas Eriksson Sep 08 '15 at 13:37
  • This was according to your requirement. " my list SHOULD include B2 and B3 since both of them overlap my ObjectA times. However, it should NOT include B1 " – M_Idrees Sep 08 '15 at 18:05
  • @MuhammadIdrees Not my question, but it is sort of strange. All three date ranges *overlap* ObjectA, while not fully enclosing. I think OP will have to clarify what he's really after. – Andreas Eriksson Sep 09 '15 at 07:45

1 Answers1

2
myList = myList.Where(x => x.StartDate < ObjectA.EndDate && x.EndDate > ObjectA.StartDate).ToList();

All objects that have begun before ObjectA ends, and hasn't ended before it started.

The code you originally wrote would only have caught objects with start and end dates that fully enclose the duration of ObjectA.

I'm confused as to why you wouldn't expect the code to return B1. It starts on 07-01 and ends on 08-01, whereas ObjectA starts on 07-31 and ends on 08-02, meaning their date ranges will overlap from 07-31 until 08-01.

  • Might want to remove the `=` part as an event that ends when `ObjectA` starts probably isn't considered to overlap. – juharr Sep 08 '15 at 12:58
  • @juharr Right you are. – Andreas Eriksson Sep 08 '15 at 13:01
  • My apologies, there was a typo in object B1 that may have clarified my question. The end date should have been '08/01/2015 00:00:00'. The object that was giving me most of my pain was the one that ended at the same time my ObjectA started and it should not have been included in my calculations. Unfortunately, my solution requires that we are time sensitive down to the minute. I ended up finding a solution in another library [link](http://www.codeproject.com/Articles/168662/Time-Period-Library-for-NET) and it works like a champ. – JamminJimE Sep 09 '15 at 11:52