How to know whether there is any overlap in a collection of periods using LINQ

后端 未结 4 656
说谎
说谎 2021-01-05 23:08

I have a collection of periods [FromDate, ToDate].

I would know whether there is any overlap between a given period and the periods in the collection.

4条回答
  •  無奈伤痛
    2021-01-05 23:43

    Approach it this way instead: There is no intersaction if the check date's todate < the from date, or the check date's fromdate > the to date. This is assuming check.from <= check.to.

    Periods.Any(p => !(check.ToDate < p.FromDate || check.FromDate > p.ToDate));
    

    or (after unwrapping the negative):

    Periods.Any(p => check.ToDate >= p.FromDate && check.FromDate <= p.ToDate));
    

提交回复
热议问题