user1740381
user1740381

Reputation: 2199

Sum database column entries in c#

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

Answers (3)

tempidope
tempidope

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

John Woo
John Woo

Reputation: 263703

you should be using SUM (LiNQ Extension Method)

int total = db.Orders.Sum(p => p.Price);

Upvotes: 1

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33381

var totalAmount = db.Orders.Sum(p => p.Price);

Upvotes: 1

Related Questions