c# compare DateTime start and DateTime stop with List<DateTime>
Like the title says:
I have a DateTime start
and a DateTime stop开发者_C百科
Now i want to compare these with a List<DateTime> dates
I just wanto check if the list dates
is between start
and stop
if(dates.all(..... >= start && <= stop)) {
. do something
}
The pseudo-code in your question is not far from the working code you're looking for.
Using LINQ's All() method:
if (dates.All(date => date >= start && date <= stop)) {
// Do something.
}
Sort the list. Check if the first item in the list is >= start date and last item in the list <= end date
With linq: if(dates.All(d => d >= start && d <= stop))
Use the All
extension method, and a lambda expression that does the comparison:
if (dates.All(d => d >= start && d <= stop)) { ... }
Incase you want to iterate just the dates in the bracket, you can use the following:
foreach (DateTime date in dates.Where(o => o >= start && o <= stop))
{
//Do your thing.
}
You can work on it like
bool allTrue = LstDates.TrueForAll(delegate(DateTime dt) { return dt >= startDate && dt <= endDate; });
精彩评论