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:
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:
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:
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:
View More:
- How to play video in ASP.NET MVC.
- Display CSV Data in ASP.NET MVC.
- Display Records using Entity Framework in ASP.NET MVC.
- Action Filters in ASP.NET MVC.
Conclusion:
I hope you would love this post. Your feedback, comments, and suggestions are always welcome to me.
Thank You.
Heya i am for the first time here. I found this board and I find It really useful & it helped me out much. I hope to give something back and aid others like you aided me.|
obviously like your web site however you need to check the spelling on several of your posts. Many of them are rife with spelling issues and I find it very troublesome to tell the truth on the other hand I will definitely come again again.|
http://chariotsforhire.org/__media__/js/netsoltrademark.php?d=www.fairporn.net/
I’m gone to say to my little brother, that he should also pay a quick visit this weblog on regular basis to take updated from most recent information.|
I loved as much as you’ll receive carried out right here. The sketch is tasteful, your authored material stylish. nonetheless, you command get bought an shakiness over that you wish be delivering the following. unwell unquestionably come more formerly again as exactly the same nearly a lot often inside case you shield this increase.|
http://t.me/s/alcohol_yakutsk