Don Juan
Don Juan

Reputation: 171

Compare DateTime Lists, c# linq

I have a List in C#

List<Dates>

public class Dates
{
    public DateTime Start {get; set;}
    public DateTime End {get; set;}
}

In List:

Start - End

  1. 2014-03-17 09:00:00 - 2014-03-17 10:00:00
  2. 2014-03-17 10:59:59 - 2014-03-17 11:44:59

How do I check if my startToCheck is between Start, the same to endToCheck to End to that list?

For example:

startToCheck = 2014-03-17 11:00:00
endToCheck = 2014-03-17 12:00:00

Obviously, my startToCheck is in List nr2, but is not find it.

I tried

if (Start <= startToCheck && End >= endToCheck)

But is not working... Any help, please?

Thanks

Upvotes: 0

Views: 1746

Answers (2)

JaredPar
JaredPar

Reputation: 754615

It sounds like you are trying to determine if any of the Dates value in the list are between that range. If so you are looking for the Any method

if (theList.Any(x => x.Start <= endToCheck && x.End >= startToCheck) { 
  ...
}

Upvotes: 1

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241485

If you want to detect all overlaps:

yourList.Where(x=> x.Start <= endToCheck && x.End >= startToCheck)

Upvotes: 0

Related Questions