我有一个SQL:
SELECT ApplicationNo,COUNT(ApplicationNo) AS CNT, SUM(Amount) as AMNT
FROM Payments where (TYPE=1 AND Position=1) and (Date>='2011-01-01')
and (Date<='2012-01-01')
GROUP BY ApplicationNo
我有没有办法在Linq中转换相同的内容?
var q = (from payments in context.Payments
where payments.Date >= fromdate && payments.Date <= todate
group payments by new { payments.ApplicationId } into g
select new
{
applicationId=g.Key,
Amount=g.Sum(a=>a.Amount)
});
如果我在Linq写了相同的内容,然后在最后分组,我得到的结果不一样。
答案 0 :(得分:1)
DateTime fromDate = new DateTime(2011, 1, 1);
DateTime toDate = new DateTime(2011, 1, 1);
var query = from p in db.Payments
where p.Type == 1 && p.Position == 1 &&
p.Date >= fromDate && p.Date <= toDate
group p by p.ApplicationNo into g
select new {
ApplicationNo = g.Key,
CNT = g.Count(),
AMNT = g.Sum(x => x.Amount)
};
此处db
是您的上下文类。