views:

82

answers:

1

Here's the SQL I would like run:

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

Can this be expressed in Linq (C#) and have it all run on the database?

+3  A: 
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();
David B
nice.. but what's wrong with the query?
Piotr Rodak
what's wrong with the mongolians?
David B
Wow...Dont you think the SQL query was much easier :-)....Anyways great job. +1
Raja
Easy isn't the only quality I value.
David B