Reputation: 2199
I am new to SQL and databases. I have a table in SQL Server. In that table there is a column price
. I want to sum all the price. What I tried is :
var totalAmount = db.Orders.Select(p => new { sum += p.Price });
What I am doing wrong here ?
Upvotes: 0
Views: 1268
Reputation: 863
You could try doing a .Sum like below:
var totalAmount = db.Orders.Select(p => p.Price ?? 0).Sum();
Similiar question at sum column with linq to sql
Upvotes: 3
Reputation: 263703
you should be using SUM
(LiNQ Extension Method)
int total = db.Orders.Sum(p => p.Price);
Upvotes: 1