我想在晚上8点到早上6点之间获取记录。如何编写where子句,以便在晚上8点到早上6点之间获取记录。 代码
model = model.OrderByDescending(x => DateTime.Parse(x.UpdatedTime)).Where(x=>x.status=="UP").Take(100).ToList();
示例数据
1 39 6/28/2017 12:08:43 PM UP
2 39 6/28/2017 12:04:18 PM UP
3 39 6/28/2017 11:49:45 AM UP
数据类型
public string UpdatedTime { get; set; }
控制器
binModel.UpdatedTime = TimeZoneInfo.ConvertTimeFromUtc(item.timestamp, TimeZoneInfo.FindSystemTimeZoneById("India Standard Time")).ToString();
答案 0 :(得分:0)
model = model.Where(x=> {
var timeOfDay = DateTime.Parse(x.UpdatedTime).TimeOfDay;
return x.status=="UP" &&
(timeOfDay.TotalHours >= 20 || timeOfDay.TotalHours < 6);
})
.OrderByDescending(x => DateTime.Parse(x.UpdatedTime)).Take(100).ToList();
应确认时间是在晚上8点或之后,早于早上6点。
答案 1 :(得分:-1)
检查DateTime.Hour超过晚上8点或更短时间
model = model.OrderByDescending(x => DateTime.Parse(x.UpdatedTime)).Where(x=>x.status=="UP" && (UpdatedTime.Hour > 20 || UpdatedTime.Hour < 6)).Take(100).ToList();