一段简单的LINQ:
View Code
var share
=
(from p
in
objDataContext.ShareDetails
where p.UserID == userID && p.OperateTime.ToString( " yyyy-MM-dd " ) == DateTime.Now.ToString( " yyyy-MM-dd " )
where p.UserID == userID && p.OperateTime.ToString( " yyyy-MM-dd " ) == DateTime.Now.ToString( " yyyy-MM-dd " )
select p).Count();
会抛出这样的异常:
方法“System.String ToString(System.String)”不支持转换为 SQL
Google查询出这样的tostring写法不可以用在条件语句中,但是时间我又只想精确到天,所以不能直接比较。
解决办法有点笨:
View Code
var share
=
(from p
in
objDataContext.ShareDetails
where p.UserID == userID && p.OperateTime.Year == DateTime.Now.Year
&& p.OperateTime.Month == DateTime.Now.Month && p.OperateTime.Day == DateTime.Now.Day
where p.UserID == userID && p.OperateTime.Year == DateTime.Now.Year
&& p.OperateTime.Month == DateTime.Now.Month && p.OperateTime.Day == DateTime.Now.Day
select p).Count();
也就是年月日分别比较,效果相同。
Lambda写法:
var share = objDataContext.ShareDetails.Where(p => p.UserID == userID
&& p.OperateTime.Year == DateTime.Now.Year
&& p.OperateTime.Month == DateTime.Now.Month
&& p.OperateTime.Day == DateTime.Now.Day).Count();
方法虽不高明,但很好用。你有什么好办法吗?请指教。