在使用EF時(shí)泊碑,想要比較字符串類型的日期時(shí)你弦,參考以下:
SQL語句:
1 select * from TableName where StartTime > '2015-04-08'
2 select * from TableName where StartTime >= '2015-04-08'
3 select * from TableName where StartTime < '2015-04-08'
4 select * from TableName where StartTime <= '2015-04-08'
Linq中的lambda表達(dá)式寫法有兩種:
第1種寫法:
1 .Where(a => a.StartTime.CompareTo('2015-04-08') > 0)
2 .Where(a => a.StartTime.CompareTo('2015-04-08') >= 0)
3 .Where(a => a.StartTime.CompareTo('2015-04-08') < 0)
4 .Where(a => a.StartTime.CompareTo('2015-04-08') <= 0)
第2種寫法:
1 .Where(a => String.Compare(a.StartTime, '2015-04-08', StringComparison.Ordinal) > 0)
2 .Where(a => String.Compare(a.StartTime, '2015-04-08', StringComparison.Ordinal) >= 0)
3 .Where(a => String.Compare(a.StartTime, '2015-04-08', StringComparison.Ordinal) < 0)
4 .Where(a => String.Compare(a.StartTime, '2015-04-08', StringComparison.Ordinal) <= 0)
經(jīng)親測,第2種寫法在EF Core中會報(bào)無法解析的錯(cuò)誤呀非,所以使用第1種即可坚俗。