---
title: "How can I calculate sum of column value in LINQ to SQL query?"  
description: "How can I calculate sum of column value in LINQ to SQL query?"  
author: "Revati S Misra"  
published: 2023-07-27  
updated: 2023-07-28  
canonical: https://www.mindstick.com/forum/159333/how-can-i-calculate-sum-of-column-value-in-linq-to-sql-query  
category: "linq"  
tags: ["linq", "linq to sql"]  
reading_time: 2 minutes  

---

# How can I calculate sum of column value in LINQ to SQL query?

How can I calculate [sum](https://www.mindstick.com/forum/159332/how-can-i-use-sum-in-linq) of [column value](https://www.mindstick.com/forum/161178/how-to-delete-duplicate-rows-based-on-single-column-value-in-mssql) in [LINQ to SQL](https://www.mindstick.com/articles/338528/how-to-use-linq-to-sql-select-query-using-c-sharp) [query](https://www.mindstick.com/blog/202/sub-query-in-sqlserver)?

## Replies

### Reply by Aryan Kumar

Sure, you can calculate the sum of a [column](https://www.mindstick.com/forum/33860/how-to-calculate-column-summary-in-sql-server) [value](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages) in a [LINQ](https://www.mindstick.com/articles/12007/language-integrated-query-linq-queries) to [SQL query](https://www.mindstick.com/forum/529/rename-table-name-and-column-name-using-sql-query) using the `Sum()` method. The syntax is as follows:

C#

```plaintext
var sum = myTable.Sum(x => x.columnValue);
```

For example, if you have a table called `products` with a column called `price`, you could use the following LINQ to SQL query to calculate the sum of all product prices:

C#

```plaintext
var sum = products.Sum(x => x.price);
```

This would return the total sum of all product prices as an integer.

Here is an example of how to calculate the sum of a column value in a LINQ to SQL query in C#:

C#

```plaintext
using System;
using System.Data.Linq;

namespace LinqToSqlSum
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a LINQ to SQL query.
            var products = new LinqToSqlSumDataContext().Products;

            // Calculate the sum of all product prices.
            var sum = products.Sum(x => x.Price);

            // Display the sum.
            Console.WriteLine("The sum of all product prices is: {0}", sum);
        }
    }

    public class LinqToSqlSumDataContext : DbContext
    {
        public LinqToSqlSumDataContext()
        {
            Database.SetInitializer(new SqlServerInitializer());
        }

        public DbSet<Product> Products { get; set; }
    }

    public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Price { get; set; }
    }
}
```

This code will first create a LINQ to SQL query for the `products` table. Then, it will calculate the sum of all product prices using the `Sum()` method. Finally, it will display the sum on the console.


---

Original Source: https://www.mindstick.com/forum/159333/how-can-i-calculate-sum-of-column-value-in-linq-to-sql-query

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
