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!