---
title: "How can I use Sum () in LINQ?"  
description: "How can I use Sum () in LINQ?"  
author: "Revati S Misra"  
published: 2023-07-27  
updated: 2023-07-28  
canonical: https://www.mindstick.com/forum/159332/how-can-i-use-sum-in-linq  
category: "linq"  
tags: ["linq", "linq to sql"]  
reading_time: 2 minutes  

---

# How can I use Sum () in LINQ?

How can I use [Sum](https://www.mindstick.com/forum/158906/what-is-the-difference-between-the-count-function-and-the-sum-function-in-sql) () in [LINQ](https://www.mindstick.com/articles/12007/language-integrated-query-linq-queries)?

## Replies

### Reply by Aryan Kumar

The `Sum()` method in LINQ is used to calculate the sum of a sequence of values. It can be used with any sequence of numeric values, such as integers, floats, doubles, and decimals.

The syntax for the `Sum()` method is as follows:

C#

```plaintext
var sum = sequence.Sum(selector);
```

The `sequence` parameter is the sequence of values that you want to sum. The `selector` parameter is a function that takes a single element from the sequence and returns a numeric value.

For example, the following code uses the `Sum()` method to calculate the sum of all the numbers in an array:

C#

```plaintext
int[] numbers = { 1, 2, 3, 4, 5 };
var sum = numbers.Sum(x => x);
```

This code will first create an array of integers called `numbers`. Then, it will use the `Sum()` method to calculate the sum of all the numbers in the array. Finally, it will store the sum in a variable called `sum`.

The `Sum()` method can also be used with LINQ queries. For example, the following code uses the `Sum()` method to calculate the sum of all the prices in a table of products:

C#

```plaintext
var products = new LinqToSqlSumDataContext().Products;
var sum = products.Sum(x => x.Price);
```

This code will first create a LINQ query for the `products` table. Then, it will use the `Sum()` method to calculate the sum of all the prices in the table. Finally, it will store the sum in a variable called `sum`.


---

Original Source: https://www.mindstick.com/forum/159332/how-can-i-use-sum-in-linq

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
