将当前时间与 WPF 中的 lambda 表达式进行比较

Compare current time with lambda expression in WPF

我想使用 lambda 表达式比较当前 DateTime.Now 与从数据库中检索的时间 (time)。

我试试这个:

string time = DateTime.Now.Hour.ToString("00") + DateTime.Now.Minute.ToString("00") + DateTime.Now.Second.ToString("00");
TimeSpan _currentTime = TimeSpan.ParseExact(time, "HHmmss", CultureInfo.InvariantCulture);

IQueryable<user> Query = _context.user.Where(s => s.userShifts.Any(s2 => s2.DayOfWeekId == _todayNumber && _currentTime >= s2.TimeFrom && _currentTime <= s2.TimeTo));

我收到以下错误:

{"Input string was not in a correct format."}

怎么了!!

好的..正确的格式是:

TimeSpan _currentTime = TimeSpan.ParseExact(time, "hh':'mm':'ss", null);

希望对您有所帮助。

使用 TimeOfDay 代替 parseExact

var _currentTime= DateTime.Now.TimeOfDay