c sharp

LINQ Sum() Function

In this article, we will discuss how to return the total sum of values through the LINQ Sum() function in List data structure.

It is also possible to use other data structures like Stack,Queue, etc.

Language Integrated Query language (LINQ) is used to perform operations on the C# collections or Normal data structures.

LINQ Sum()

Sum() in LINQ is used to return the total sum of elements.

Syntax:

If the values are single:

list.Sum()

If there are multiple values:

list.Sum(element => element.variable)

element iterates the values in a list and a variable is one of the values present in a list.

Let’s explore this method.

Example 1:

Here, we will create a list that holds integer elements. We will use Sum() to return the total sum of these elements.

using System;
using System.Linq;
using System.Collections.Generic;

 //create a class - Linuxhint
class Linuxhint
{
 
    static public  void Main(){
       
       //create data
       List<int> first_list = new List<int>() {120,80,45,123,456,45,120,8,9,0 };
       
       
       Console.WriteLine("List: ");
           foreach (var values in first_list)
            {
                Console.WriteLine(values);
            }
           
           
        //get the sum of elements in the list
       var total = first_list.Sum();
           
         Console.WriteLine("Sum of elements from the above list: "+total);
         
}
}

Output:

Explanation:

1. So first, we created a list data structure with 10 integers.

2. After that, we applied Sum() to get the sum of these elements.

3. Finally, we can display the result.

Example 2:

Let’s create Food that holds three attributes – food_price, name, and quantity.

Get sum of food_price and quantity attributes separately.

using System;
using System.Linq;
using System.Collections.Generic;

 //create a class - Linuxhint
class Linuxhint
{
    //define the data for Food
   class Food  
    {  
        public int food_price { get; set; }  
        public string name { get; set; }  
        public int quantity { get; set; }  
    }
    static public  void Main(){
       
       //create data
       List<Food> first_list = new List<Food>();
       //add values
       first_list.Add(new Food { food_price=300,name="parota",quantity=1 });
       first_list.Add(new Food { food_price=800,name="paneer",quantity=4 });
       first_list.Add(new Food { food_price=100,name="mushroom",quantity=2 });
       first_list.Add(new Food { food_price=564,name="vegtables",quantity=10 });
       first_list.Add(new Food { food_price=400,name="fruits",quantity=8 });
       
       foreach (var value in first_list)  
            {  
                Console.WriteLine(value.food_price+"->"+value.name+"->"+value.quantity);  
            }  
           
           
           
        Console.WriteLine("-----------------------LINQ Sum-----------------------");

         //Total sum of food_price values
       var total1 = first_list.Sum(element => element.food_price);  
             
       Console.WriteLine("Sum of food_price: "+total1);
       
         //Total sum of quantity values
       var total2 = first_list.Sum(element => element.quantity);  
             
       Console.WriteLine("Sum of quantity: "+total2);
           
           
}
}

Output:

Explanation:

1. First, we have to declare the structure:

So, we defined three attributes with food_price and quantity as an integer type and name as a string type.

2. Next, we created a list named first_list from the structure-Food.

3. Add the values to the above created list.

4. Apply Sum() to return the total sum of values in food_price and quantity attributes.

Conclusion

In this C# – LINQ tutorial, we saw how to return the total sum of the data with the Sum() function. It is also possible to return the total sum for the particular attribute in the given data. We discussed two different examples to understand the concept better.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain