我需要从表中选择日期间隔的记录。
但在这种模式中,要选择的日期保留为nvarchar
20160511_155015 (yyyymmdd_hhmmss)
我不能使用ToList()
使其成为DateTime.ParseExact(entry.StartDate, "yyyyMMdd_HHmmss", CultureInfo.InvariantCulture)
该表保存了数百万条记录所以我需要做这样的东西:
var preQuery = context.Table
.AsNoTracking()
.Select(x => new
{
StartDate = ConvertFrom()),
Mode = x.Mode,
SessionStart = x.AStart,
SessionEnd = x.AEnd,
x.SensorNumber
})
.Where(x => x.StartDate != null
&& x.StartDate >= startDate
&& x.StartDate <= endDate)
.ToList();
是否可以将字符串表示形式转换为Datetime,然后继续使用Where子句
请尝试此
DateTime.ParseExact(dateTime ,format,CultureInfo.InvariantCulture);
var preQuery = context.Table.AsNoTracking().Select(x => new
{
StartDate = ConvertFrom(),
Mode = x.Mode,
SessionStart = DateTime.ParseExact(x.AStart,"yyyyMMdd_HHmmss", CultureInfo.InvariantCulture),
SessionEnd = DateTime.ParseExact(x.AEnd,"yyyyMMdd_HHmmss", CultureInfo.InvariantCulture),
x.SensorNumber
})
.Where(x => x.StartDate != null && x.StartDate >= startDate && x.StartDate <= endDate)
.ToList();
您可以尝试:StartDate.StartsWith("20160511")左右。。或Convert.ToInt32(StartDate.Substring(0.8))>20160511
顺便说一句,我认为您可能需要运行一个脚本,只要它需要,并创建一个新列,该列将基于该列