我有这个sql查询
SELECT SUM(DATEDIFF(MINUTE, InTime , OutTime)) /60
FROM Attendance
WHERE InTime BETWEEN '01-01-2016' AND '01-31-2016'
AND Employee=63
var AttendofEmp = (from ab in db.Attendances
where ab.InTime >= Convert.ToDateTime("01-01-2016") &&
ab.InTime >= Convert.ToDateTime("01-31-2016")
select new { ab.InTime });
它对sql查询运行良好,但当我使用linq查询来获得相同的结果时,它会出现错误
提前感谢。。。
尝试使用new DateTime()
作为日期常量。此外,您可以使用SqlMethods.DateDiffMinute
来获得分钟差,使用.Sum()
来获得总和:
var AttendofEmp =
(from a in db.Attendances
where a.InTime >= new DateTime(2016, 1, 4) && a.InTime <= new DateTime(2016, 1, 31)
select SqlMethods.DateDiffMinute(a.InTime , a.OutTime)
).Sum() / 60;
将Convert.ToDateTime
转换为变量,然后在查询中使用它。
var Date1 = Convert.ToDateTime("01-01-2016");
var Date2 = Convert.ToDateTime("01-31-2016")
var AttendofEmp = (from ab in db.Attendances
where ab.InTime >= Date1 &&
ab.InTime >= Date2
select new { ab.InTime });