初学者LINQ语法问题

时间:2009-11-12 14:02:10

标签: linq-to-sql

我有一个基本的SQL表( pKey INT, TransDate smallDateTime, Amount Float) 我只是想在LINQ中模拟这个SQL

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

我为此创建了LINQ dbml,我可以获得日期范围的基本查询结果

但是我找不到正确的语法来获取日期范围内的SUM,我已经尝试了以下几种变体,但要么他们不编译,要么他们给出的结果不能转换为双

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;

1 个答案:

答案 0 :(得分:2)

这应该有效:

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