tags:

views:

29

answers:

1

I have a basic SQL Table ( pKey INT, TransDate smallDateTime, Amount Float) I simply want to emulate this SQL in LINQ

SELECT SUM(Amount) AS result
FROM dbo.Basic 
WHERE TransDate >= @startDate
AND TransDate <= @EndDate

I have created the LINQ dbml for this and I can get basic query results for a date range

However I can't find the right syntax to get the SUM over a dateRange, I've tried several variations on the following but either they dont compile, or the result they give cannot be converted to a double

BasicDataContext dContext = new BasicDataContext();
var lnq = from c in dContext.Basic
      where c.TransDate >= startdate &&
            c.TransDate <= enddate
      select new { total = c.Sum(Amount) };

double result = (double)lnq.total;
+2  A: 

This should work:

double result =  (from c in dContext.Basic
                   where c.TransDate >= startdate &&
                         c.TransDate <= enddate
                   select c.Amount).Sum();
Kobi