EF中使用Linq的Lambda比较字符串格式日期大小

在使用EF时,想要比较字符串类型的日期时,参考以下:

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‘

lambda表达式写法:
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)

更规范的写法:
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)

  

posted @ 2018-08-30 10:13  齐建伟  阅读(1777)  评论(0编辑  收藏  举报