问题:Linq中比较两个时间的年月日部分
表中某个字段的时间和系统时间比较大小(只比较年月日)
思路一:转换成字符串比较
var queryable = dbContext.Table .Where(p => p.ETime.ToString("yyyy-MM-dd") >= DateTime.Now.ToString("yyyy-MM-dd")
异常:字符串无法比较
思路二:先转换成字符串,再转换成DataTime比较
var queryable = dbContext.Table .Where(p => Convert.ToDateTime(p.ETime.ToString("yyyy-MM-dd")) >= Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"))
异常:调用ToDateTime方法失败
思路三:当前时间和系统时间(系统时间转换成仅包含年月日)
DateTime dtToday = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")); var queryable = dbContext.Table .Where(p => p.ETime >= dtToday)
Sqlserver中按日期的年月日比较:
CONVERT(varchar(12),v.ETime,112)>=CONVERT(varchar(12),GETDATE(),112)
http://blog.csdn.net/quxiao2002/article/details/7360542
本月内+今天
private DateTime dtMonthFirstday = Convert.ToDateTime(new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("yyyy-MM-dd"));
private DateTime dtToday = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
private DateTime dtNexDay = Convert.ToDateTime(DateTime.Now.AddDays(1).ToString("yyyy-MM-dd"));
--今天
Where(p => p.Created >= dtToday && p.Created < dtNexDay).Count();
当月
Where(p => p.Created >= dtMonthFirstday).Count();