Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check List<> Contains with comparison on TimeSpan

Ive got a list of TimeSpans and I need to check if any of them are more than 5 hrs. I know I could just loop through the list, but if its possible I'd prefer to check for the condition using one of the built in functions for List<>.

if (driverSchedules.GetAllShifts().Contains(delegate(TimeSpan ts) { return ts > new TimeSpan(5,0,0);}))
{
    return true;
}

*GetAllShifts returns a List.

The error I'm getting says:

"Cannot convert anonymous method to type 'System.TimeSpan' because it is not a 
delegate type"

As far as I can tell the "delegate type" is System.TimeSpan

like image 680
Kram Isterpf Avatar asked Nov 04 '22 14:11

Kram Isterpf


1 Answers

1.: Contains() doesn't take a delegate as an argument; it wants a specific TimeSpan value.

2.: Since you want to find out whether any item in the collection matches a particular condition, use Any(); it accepts a delegate.

return driverSchedules.GetAllShifts().Any(ts => ts > new TimeSpan(5,0,0));
like image 126
canon Avatar answered Nov 14 '22 05:11

canon