在Linq中对Sql进行求和和减法

时间:2013-11-21 14:54:06

标签: linq entity-framework

大家好我想要实现像Linq到Sql的SQL Select语句。任何帮助将不胜感激。

SELECT SUM(Debit-Credit) AS LBalance FROM dbo.LeaveLedger
WHERE StaffId =1 AND LYEAR='2000'

3 个答案:

答案 0 :(得分:3)

假设实体框架:

Context.Table.Where(x => x.StaffId == 1 and x.LYEAR == "2000")
   .Sum(y => (y.Debit - y.Credit));

答案 1 :(得分:2)

这样的东西?

var sum = 
    db.LeaveLedger
      .Where(ll => ll.StaffId == 1 and ll.LYEAR == "2000")
      .Sum(ll => (ll.Debit - ll.Credit))

答案 2 :(得分:2)

由于Mansfield已经显示了表达式语法,我将使用经典查询:

var LBalance = (from p in dbo.LeaveLedger
                where p.StaffId == 1 && p.LYEAR == "2000"
                select (p.Debit - p.Credit).Sum();