这是我想要运行的SQL:
DECLARE @StartDt DATETIME, @EndDt DATETIME
SET @StartDt='2009-01-01'
SET @EndDt='2010-06-01'
SELECT
AVG(DATEDIFF(day, CreatedDt, CompletedDt))
AVG(DATEDIFF(day, CreatedDt, ComplianceDt))
FROM MyTable
WHERE RequestDt BETWEEN @StartDt AND @EndDt
这可以用Linq(C#)表示并让它全部在数据库上运行吗?
答案 0 :(得分:3)
DateTime startDt = new DateTime(2009, 1, 1);
DateTime endDt = new DateTme(2010, 6, 1);
var query = dc.MyTables
.Where(x => startDt <= x.RequestDate && x.RequestDt <= endDt)
.GroupBy(x => 1) //unsure about this group by
.Select(g => new
{
FirstAvg = g.Average(x =>
SqlMethods.DateDiffDay(x.CreatedDt, x.CompletedDt)),
SecondAvg = g.Average(x =>
SqlMethods.DateDiffDay(x.CreatedDt, x.ComplianceDt))
});
var row = query.Single();