In this tutorial, I am going to explain you about sum() method in LINQ. Like SQL, Linq also provides many aggregate operators to perform aggregation on data. Here we are going to see how we can use sum() method in Linq.

Sum() in Linq:

sum() method is used to calculate the sum of all integer values in the collection. It is similar to sum aggregate function in SQL Server.

Example 1:

The following example just calculates the sum of all item of a collection.

IList<int> lstCollection = new List<int>() { 45, 23, 243, 432, 44, 21 };

var iTotal = lstCollection.Sum();

Console.WriteLine("Sum: {0}", iTotal);

Output:

Sum()

Example 2:

The following example will calculate the sum of all event and odd number.

IList<int> lstCollection = new List<int>() { 45, 23, 243, 432, 44, 21 };

var iEvenSum = lstCollection.Sum(i =>
{
if (i % 2 == 0)
return i;
else
return 0;

});
var iOddSum = lstCollection.Sum(i =>
{
if (i % 2 != 0)
return i;
else
return 0;

});


Console.WriteLine("Sum: of Even Number {0}", iEvenSum);

Console.WriteLine("Sum: of Odd Number {0}", iOddSum);

Output:

Sum()

Example 3:

The following example will calculate the total number of student of the particular class.

IList<Student> lstStudent = new List<Student>() { 
new Student() { StudentID = 1, StudentName = "Vivke", Class = 2} ,
new Student() { StudentID = 2, StudentName = "Rohan", Class = 4 } ,
new Student() { StudentID = 3, StudentName = "Kavita", Class = 5 } ,
new Student() { StudentID = 4, StudentName = "Rishab" , Class = 5} ,
new Student() { StudentID = 5, StudentName = "Karan" , Class = 6 },
new Student() { StudentID = 6, StudentName = "Vikas" , Class = 5 },
new Student() { StudentID = 7, StudentName = "Akshad" , Class = 5 }
};
Console.WriteLine("Enter a class");
int iClass = Convert.ToInt32(Console.ReadLine());
int iTotalStudent = lstStudent.Sum(i =>
{
if (i.Class == iClass)
return 1;
else
return 0;
});
Console.WriteLine("Total Student of Class {0} is {1}", iClass, iTotalStudent);

class Student
{
public int StudentID { get; set; }
public string StudentName { get; set; }
public int Class { get; set; }

}

Output:

Sum()

Example 4:

The following example will calculate the sum of Marks of all Student using the Linq Query.

IList<Student> lstStudent = new List<Student>() { 
new Student() { StudentID = 1, StudentName = "Vivke", Class = 2,Marks=39} ,
new Student() { StudentID = 2, StudentName = "Rohan", Class = 4,Marks=66 } ,
new Student() { StudentID = 3, StudentName = "Kavita", Class = 5,Marks=77 } ,
new Student() { StudentID = 4, StudentName = "Rishab" , Class = 5,Marks=77} ,
new Student() { StudentID = 5, StudentName = "Karan" , Class = 6 ,Marks=45},
new Student() { StudentID = 6, StudentName = "Vikas" , Class = 5 ,Marks=88},
new Student() { StudentID = 7, StudentName = "Akshad" , Class = 5,Marks=55 }
};
int iTotal = (from c in lstStudent select c.Marks).Sum();
Console.WriteLine("Sum of Marks {0}", iTotal);

class Student
{
public int StudentID { get; set; }
public string StudentName { get; set; }
public int Class { get; set; }
public int Marks { get; set; }

}

Output:

Sum()

View More:

Conclusion:

I hope you would love this post. Your feedback, comments, and suggestions are always welcome to me.

Thank You.

Leave a Comment