Tom Brown
Tom Brown

Reputation: 793

Beginner LINQ syntax question

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;

Upvotes: 0

Views: 98

Answers (1)

Kobi
Kobi

Reputation: 138017

This should work:

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

Upvotes: 2

Related Questions