locked
Checking overlaps on dates in collection items RRS feed

  • Question

  • Hi,

    I was given a task to check the overlaps of the dates withing the collection.  For instance if I have Start and End dates for items how can I check if the End date of the 1st item is less than or equal to the 2nd Start date.

    I haven't worked out, but I guess if I sort the collection I might be able to do it in for loop construct.  I was wondering if this possible to do in Linq?

    Any suggestions please as I haven't found any solution yet.

    Thanks

    Tuesday, January 12, 2016 12:20 PM

Answers

  • If you are not interested in overlap details, then try this:

    bool contains_overlaps = items.Any( a => items.Any( b => b != a && !( ( a.Start.Date >= b.End.Date || b.Start.Date >= a.End.Date ) ) ) );

    It does not assume that the collection is ordered.

    It also ignores the time parts. In order to consider the time, remove ‘.Date’.



    • Edited by Viorel_MVP Tuesday, January 12, 2016 1:09 PM
    • Marked as answer by Alex Teslin Tuesday, January 12, 2016 1:30 PM
    Tuesday, January 12, 2016 1:07 PM

All replies

  • I don't think that your task should allow the sorting of the collection based on the date field, because after that your validation will pass for sure (The End date of the 1st item is less than or equal to the 2nd Start date), right?

    Also are you sure about the rule that you have to check record 1 with 2 then 2 with 3...What if record 1 date overlaps with record [Edit] 5 date.



    Fouad Roumieh


    Tuesday, January 12, 2016 1:00 PM
  • If you are not interested in overlap details, then try this:

    bool contains_overlaps = items.Any( a => items.Any( b => b != a && !( ( a.Start.Date >= b.End.Date || b.Start.Date >= a.End.Date ) ) ) );

    It does not assume that the collection is ordered.

    It also ignores the time parts. In order to consider the time, remove ‘.Date’.



    • Edited by Viorel_MVP Tuesday, January 12, 2016 1:09 PM
    • Marked as answer by Alex Teslin Tuesday, January 12, 2016 1:30 PM
    Tuesday, January 12, 2016 1:07 PM
  • Thanks for your reply Fouad,

    Well, I thought if i sort the list with Start theby End dates first.  Then in the for loop comparing the Start and End dates of the 1st item with Start and End dates of the adjacent second item will return true or false.  My thought was this way I will need only one loop.  And if the collection is not sorted then I will require nested loop.

    I am now realising that this is not a straightforward task and certainly cannot be implemented only with the linq query, unless I can be wrong of course.

    Tuesday, January 12, 2016 1:17 PM
  • I believe your solution works Viorel.  I've tested with dates and it seems it does produce the correct result.  This is great.  Now, I will need to amend slightely to include Time as well.

    I didn't think it would be possible to do it with just on line of code )))

    Thank you

    Tuesday, January 12, 2016 1:30 PM
  • Brilliant, it does work with DateTime as well.

    Thank you

    Tuesday, January 12, 2016 1:37 PM