如何编写linq查询,仅在上午9点到下午5点之间获取记录。除此之外的记录应该被丢弃。
timestamp数据类型
码
var items = Pirs.Where(a => !a.dataFrame.EndsWith("AAAAAAAAAAA=") && (fromDate == null || fromDate.Value.Date <= TimeZoneInfo.ConvertTimeFromUtc(Convert.ToDateTime(a.timestamp), TimeZoneInfo.FindSystemTimeZoneById("India Standard Time")).Date) && (toDate == null || toDate.Value.Date >= TimeZoneInfo.ConvertTimeFromUtc(Convert.ToDateTime(a.timestamp), TimeZoneInfo.FindSystemTimeZoneById("India Standard Time")).Date))
.GroupBy(a => a.dataFrame.Substring(a.dataFrame.Length - 12))
.Select(g => g.First()).OrderBy(a => a.timestamp);
答案 0 :(得分:5)
Pirs.Where(a.timestamp.TimeOfDay > new TimeStamp(9, 0, 0) && //all times after 9am
a.timestamp.TimeOfDay < new TimeStamp(17, 0, 0) && //all times before 5pm
a.timestamp.Date > fromDate && //all dates after fromData
a.timestamp.Date < toDate) //all dates before toDate
答案 1 :(得分:4)
在条件
中执行以下操作TimeSpan span = TimeSpan.Parse("09:00:00");
TimeSpan espan = TimeSpan.Parse("17:00:00");
Pirs.Where(a => a.timestamp >= startDate && a.timestamp <= toDate && a.timestamp.TimeOfDay >= span && a.timestamp.TimeOfDay <= espan);