如何在Linq中使用Sum()?

时间:2012-11-05 10:34:22

标签: c# mysql linq

如何将此语句转换为LINQ?

SELECT Sum(Balance) FROM account WHERE name='stocks' AND userid=290;

3 个答案:

答案 0 :(得分:7)

decimal sumLineTotal = (from od in account
                        where name == 'stocks' && userid == 290
                        select Balance).Sum();

答案 1 :(得分:4)

var sum = db.account.Where(a => a.name == "stocks" && a.userid == 290)
                    .Sum(a => a.Balance);

答案 2 :(得分:0)

像这样:

myBalanceSum = account.Where(x => x.name == 'stocks' && x.userid == 290 ).Sum(x => x.Balance);