user1719311
user1719311

Reputation: 111

How can I use Sum() in Linq?

How do I convert this statement into LINQ?

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

Upvotes: 7

Views: 12269

Answers (3)

Gary Barrett
Gary Barrett

Reputation: 2018

Like this:

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

Upvotes: 0

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236208

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

Upvotes: 6

John Woo
John Woo

Reputation: 263703

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

Upvotes: 10

Related Questions