为什么我的 linq 查询没有给我预期的结果
why my linq query is not giving me expected results
我有这个 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 });
我有这个 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 });