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.

Explore post tags

145 Comments

  • I simply want to tell you that I’m beginner to blogging and site-building and seriously loved you’re web page. Most likely I’m likely to bookmark your blog . You amazingly come with really good posts. Kudos for revealing your web site.

  • A further issue is that video games can be serious naturally with the key focus on studying rather than entertainment. Although, it has an entertainment part to keep your children engaged, every single game is normally designed to work towards a specific experience or curriculum, such as math or scientific disciplines. Thanks for your post.

  • Sapid Agency is a Search Engine Optimization company in New York City that provides SEO Services. Their proprietary SEO strategies help struggling websites and aspiring business owners to rank their websites higher in multiple search engines like Google , Yahoo and Bing. They provide local and gmb map ranking for businesses in NYC and many other local areas. Find more at https://www.sapidagency.com/ @ 145 E 57TH NEW YORK, NY 10022, USA, +1 971 341 5608 USA

  • Oh my goodness! a huge article dude. Thanks However I am experiencing situation with ur rss . Don’t know why Not able to sign up to it. Can there be anyone getting similar rss problem? Anybody who is conscious of kindly respond. Thnkx

  • I need to to thank you for this good read!! I certainly enjoyed every bit of it. I’ve got you book marked to look at new stuff you post…

  • Having read this I believed it was really enlightening. I appreciate you taking the time and effort to put this short article together. I once again find myself spending a lot of time both reading and posting comments. But so what, it was still worthwhile.

  • I blog frequently and I really appreciate your content. This great article has really peaked my interest. I am going to book mark your website and keep checking for new details about once a week. I opted in for your RSS feed as well.

  • Good day! I could have sworn I’ve been to this site before but after looking at a few of the articles I realized it’s new to me. Anyways, I’m definitely delighted I discovered it and I’ll be book-marking it and checking back often.

  • Hi there! This blog post could not be written any better! Looking at this post reminds me of my previous roommate! He constantly kept talking about this. I will forward this information to him. Fairly certain he’s going to have a good read. I appreciate you for sharing!

  • I have observed that in the world the present moment, video games include the latest phenomenon with kids of all ages. Many times it may be not possible to drag your son or daughter away from the video games. If you want the very best of both worlds, there are lots of educational games for kids. Interesting post.

  • I’m excited to find this great site. I wanted to thank you for your time just for this fantastic read!! I definitely really liked every part of it and I have you book marked to see new things in your site.

  • An intriguing discussion is definitely worth comment. I believe that you ought to publish more on this subject matter, it might not be a taboo matter but usually folks don’t talk about these topics. To the next! Kind regards.

  • Greetings, I believe your website could be having internet browser compatibility problems. When I take a look at your site in Safari, it looks fine but when opening in I.E., it’s got some overlapping issues. I merely wanted to give you a quick heads up! Aside from that, excellent website!

  • Greetings! Very useful advice in this particular post! It is the little changes that produce the largest changes. Thanks for sharing!

  • I needed to thank you for this fantastic read!! I certainly loved every little bit of it. I have got you book-marked to check out new things you post…

  • Sapid Agency is a Search Engine Optimization company in New York City that provides SEO Services. Their proprietary SEO strategies help struggling websites and aspiring business owners to rank their websites higher in multiple search engines like Google , Yahoo and Bing. They provide local and gmb map ranking for businesses in NYC and many other local areas. Find more at https://www.sapidagency.com/ @ 145 E 57TH NEW YORK, NY 10022, USA, +1 971 341 5608 USA

  • MetroClick specializes in building completely interactive products like Photo Booth for rental or sale, Touch Screen Kiosks, Large Touch Screen Displays , Monitors, Digital Signages and experiences. With our own hardware production facility and in-house software development teams, we are able to achieve the highest level of customization and versatility for Photo Booths, Touch Screen Kiosks, Touch Screen Monitors and Digital Signage. Visit MetroClick in NYC at http://www.metroclick.com/ or , 121 Varick St, New York, NY 10013, +1 646-843-0888.

  • Nice post. I learn something totally new and challenging on sites I stumbleupon everyday. It’s always helpful to read through content from other writers and use a little something from other web sites.

  • This is a topic that is near to my heart… Best wishes! Exactly where are your contact details though?

  • Hi, There’s no doubt that your site may be having internet browser compatibility problems. Whenever I take a look at your website in Safari, it looks fine however, if opening in I.E., it has some overlapping issues. I simply wanted to give you a quick heads up! Besides that, wonderful site!

  • Howdy, There’s no doubt that your blog might be having browser compatibility issues. Whenever I look at your website in Safari, it looks fine however when opening in I.E., it has some overlapping issues. I just wanted to provide you with a quick heads up! Other than that, fantastic website.

  • You’ve made some really good points there. I checked on the net for more info about the issue and found most individuals will go along with your views on this web site.

  • I merely want to show you that I am new to writing a blog and utterly valued your article. Very possible I am prone to store your blog post . You really have magnificent article material. Get Pleasure From it for giving out with us your favorite domain webpage

  • I really like your writing style, excellent information, appreciate it for putting up :D. “Much unhappiness has come into the world because of bewilderment and things left unsaid.” by Feodor Mikhailovich Dostoyevsky.

  • I like this post, enjoyed this one thank you for posting. “No trumpets sound when the important decisions of our life are made. Destiny is made known silently.” by Agnes de Mille.

  • Great blog you’ve got here.. It’s hard to find quality writing like yours these days. I seriously appreciate individuals like you! Take care!!

  • I’m excited to discover this web site. I need to to thank you for ones time for this particularly fantastic read!! I definitely loved every bit of it and I have you book marked to see new things on your web site.

  • Enjoyed looking through this, very good stuff, thanks . “It is well to remember that the entire universe, with one trifling exception, is composed of others.” by John Andrew Holmes.

  • Hi there! I just want to give you a big thumbs up for the excellent info you’ve got right here on this post. I’ll be returning to your website for more soon.

  • I like what you guys are up too. Such smart work and reporting! Keep up the excellent works guys I’ve incorporated you guys to my blogroll. I think it will improve the value of my site :).

  • Hello there! I just wish to give a massive thumbs up for the fantastic information you have here on this message. I will be coming back to your blog site for more soon.

  • This site was… how do you say it? Relevant!! Finally I’ve found something that helped me. Kudos!

  • I’m extremely pleased to discover this great site. I want to to thank you for your time due to this wonderful read!! I definitely liked every part of it and I have you book marked to check out new things in your website.

  • Howdy! This article could not be written any better! Looking at this post reminds me of my previous roommate! He continually kept talking about this. I am going to forward this post to him. Fairly certain he will have a very good read. Many thanks for sharing!

  • I like reading a post that can make men and women think. Also, thanks for allowing for me to comment!

  • Sapid Agency is a Search Engine Optimization company in New York City that provides SEO Services. Their proprietary SEO strategies help struggling websites and aspiring business owners to rank their websites higher in multiple search engines like Google , Yahoo and Bing. They provide local and gmb map ranking for businesses in NYC and many other local areas. Find more at https://www.sapidagency.com/ @ 145 E 57TH NEW YORK, NY 10022, USA, +1 971 341 5608 USA

  • Great info. Lucky me I ran across your website by accident (stumbleupon). I have book marked it for later!

  • I really desire to show you that I am new to blogging and very much cherished your post. Likely I am prone to remember your blog post . You definitely have impressive article materials. Be Grateful For it for telling with us your own internet site webpage

  • Aw, this was a really good post. Taking the time and actual effort to create a great article… but what can I say… I put things off a whole lot and never manage to get anything done.

  • MetroClick specializes in building completely interactive products like Photo Booth for rental or sale, Touch Screen Kiosks, Large Touch Screen Displays , Monitors, Digital Signages and experiences. With our own hardware production facility and in-house software development teams, we are able to achieve the highest level of customization and versatility for Photo Booths, Touch Screen Kiosks, Touch Screen Monitors and Digital Signage. Visit MetroClick in NYC at http://www.metroclick.com/ or , 121 Varick St, New York, NY 10013, +1 646-843-0888.

  • Can I just say what a relief to discover an individual who really understands what they are talking about online. You actually realize how to bring a problem to light and make it important. More and more people need to check this out and understand this side of the story. It’s surprising you aren’t more popular because you definitely possess the gift.

  • You’ll find it practically close to impossible to see well-qualified readers on this matter, yet somehow you look like you fully grasp what you’re covering! Thanks A Lot

  • Faytech North America is a touch screen Manufacturer of both monitors and pcs. They specialize in the design, development, manufacturing and marketing of Capacitive touch screen, Resistive touch screen, Industrial touch screen, IP65 touch screen, touchscreen monitors and integrated touchscreen PCs. Contact them at http://www.faytech.us, 121 Varick Street, New York, NY 10013, +1 646 205 3214.

  • Howdy! I could have sworn I’ve visited this blog before but after going through some of the posts I realized it’s new to me. Regardless, I’m definitely delighted I found it and I’ll be book-marking it and checking back frequently!

  • It’s mostly close to impossible to see well-qualified people on this niche, unfortunately you appear like you know those things you’re posting on! With Thanks

  • Wow, marvelous blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your site is wonderful, as well as the content!|

  • I just want to reveal to you that I am new to blog posting and undeniably valued your article. Probably I am likely to remember your blog post . You definitely have fabulous article material. Truly Appreciate it for share-out with us your favorite url write-up

  • This site was… how do I say it? Relevant!! Finally I have found something that helped me. Many thanks!|

  • After I originally commented I seem to have clicked the -Notify me when new comments are added- checkbox and now whenever a comment is added I receive 4 emails with the same comment. There has to be a way you are able to remove me from that service? Thanks!

  • When some one searches for his vital thing, therefore he/she needs to be available that in detail, therefore that thing is maintained over here.|

  • Heya i’m for the first time here. I found this board and I find It truly useful & it helped me out much. I hope to give something back and help others like you helped me.|

  • Leading Online Pharmacy – Contact us at +1 (917) 259-3352 for unbelievable rates, discount and offers on any medicine. Get it delivered free of cost at your door steps, call us today. Phone : +1 (917) 259-3352

  • Hi there, There’s no doubt that your blog could possibly be having browser compatibility issues. Whenever I look at your blog in Safari, it looks fine but when opening in I.E., it has some overlapping issues. I just wanted to provide you with a quick heads up! Aside from that, excellent blog!

  • With havin so much written content do you ever run into any problems of plagorism or copyright infringement? My website has a lot of exclusive content I’ve either written myself or outsourced but it seems a lot of it is popping it up all over the internet without my agreement. Do you know any solutions to help prevent content from being stolen? I’d certainly appreciate it.

  • Leading Digital Marketing agency in India and Kolkata, Leading Web Design company. Providing web designing and Digital marketing services, Digital marketing course, and certification with an internship.

  • You’re so cool! I don’t think I have read anything like this before. So nice to find another person with a few original thoughts on this subject matter. Seriously.. many thanks for starting this up. This web site is something that is required on the web, someone with a bit of originality!

  • Good day! I simply would like to offer you a huge thumbs up for the excellent info you’ve got here on this post. I’ll be coming back to your web site for more soon.

  • Right here is the perfect site for anyone who hopes to find out about this topic. You know so much its almost tough to argue with you (not that I personally would want to…HaHa). You certainly put a brand new spin on a subject which has been written about for ages. Wonderful stuff, just great!

  • A remarkable share, I simply provided this onto a coworker that was doing a little analysis on this. And also he in fact acquired me morning meal because I found it for him. smile. So let me rephrase that: Thnx for the treat! But yeah Thnkx for spending the moment to discuss this, I really feel highly about it and like learning more on this subject. Ideally, as you come to be competence, would certainly you mind upgrading your blog site with more details? It is highly practical for me. Big thumb up for this blog post!

  • Aw, this was an actually nice article. In suggestion I want to place in writing like this additionally? taking time and actual initiative to make an excellent article? however what can I claim? I procrastinate alot and also never seem to obtain something done.

  • A remarkable share, I just offered this onto a colleague that was doing a little analysis on this. As well as he actually acquired me breakfast due to the fact that I found it for him. smile. So let me reword that: Thnx for the treat! However yeah Thnkx for spending the time to review this, I feel strongly concerning it and enjoy learning more on this topic. If possible, as you become expertise, would certainly you mind updating your blog with more information? It is very valuable for me. Huge thumb up for this article!

  • I’m the proprietor of JustCBD Store company (justcbdstore.com) and I am currently planning to broaden my wholesale side of company. I really hope that someone at targetdomain share some guidance . I considered that the best way to accomplish this would be to talk to vape companies and cbd stores. I was hoping if someone could recommend a trusted web-site where I can buy CBD Shops Business Mailing List I am currently checking out creativebeartech.com, theeliquidboutique.co.uk and wowitloveithaveit.com. Unsure which one would be the best solution and would appreciate any guidance on this. Or would it be easier for me to scrape my own leads? Ideas?

  • Hello there! I simply wish to offer a massive thumbs up for the fantastic details you have here on this blog post. I will certainly be coming back to your blog site for even more quickly.

  • That is a great tip particularly to those new to the blogosphere. Brief but very precise info… Many thanks for sharing this one. A must read article!

  • An interesting discussion deserves remark. I assume that you need to write extra on this topic, it could not be a forbidden subject but generally people are insufficient to speak on such topics. To the next. Cheers

  • This is the perfect website for everyone who wants to understand this topic. You realize a whole lot its almost tough to argue with you (not that I personally will need to…HaHa). You certainly put a brand new spin on a topic that has been discussed for many years. Wonderful stuff, just great!

  • Having read this I thought it was rather informative. I appreciate you finding the time and energy to put this informative article together. I once again find myself spending way too much time both reading and posting comments. But so what, it was still worth it!|

  • When I initially commented I clicked the -Inform me when new comments are added- checkbox as well as now each time a remark is included I get 4 e-mails with the exact same comment. Is there any way you can remove me from that solution? Thanks!

  • An impressive share! I’ve just forwarded this onto a co-worker who has been conducting a little homework on this. And he actually bought me lunch due to the fact that I discovered it for him… lol. So allow me to reword this…. Thank YOU for the meal!! But yeah, thanx for spending some time to discuss this topic here on your site.

  • I merely desire to notify you that I am new to putting up a blog and incredibly adored your page. Probably I am probably to bookmark your blog post . You indeed have memorable article material. Like it for telling with us your current internet document

  • There is definately a great deal to learn about this topic. I like all the points you made.

  • I?d have to contact you right here. Which is not something I usually do! I take pleasure in reviewing a post that will make people assume. Likewise, many thanks for permitting me to comment!

  • Heya there, just turned out to be aware of your website through yahoo, and discovered that it’s quite informative. I’ll value if you keep up this idea.

  • You can definitely see your skills in the article you write. The world hopes for more passionate writers such as you who are not afraid to say how they believe. All the time go after your heart.|

  • Aw, this was an actually wonderful post. In concept I want to place in writing such as this furthermore? taking some time and also actual effort to make a very good write-up? but what can I say? I hesitate alot as well as by no means appear to obtain something done.

  • Spot on with this write-up, I absolutely assume this internet site needs far more factor to consider. I?ll probably be again to read much more, thanks for that details.

  • Hello! I could have sworn I’ve visited your blog before but after looking at many of the posts I realized it’s new to me. Regardless, I’m certainly pleased I found it and I’ll be bookmarking it and checking back often!

  • After exploring a number of the blog posts on your website, I honestly like your way of writing a blog. I saved it to my bookmark site list and will be checking back soon. Please check out my web site too and tell me what you think.

  • Hello i am so delighted I discovered your blog, I actually discovered you by error, while I was searching Yahoo for something else, Anyways I am here now and would just like to say thanks for a great blog posting and a all round absorbing blog (I also love the theme/design), I do not have time to read it all at the right now but I have bookmarked it and also added your RSS feeds, so when I have time I will be back to read more.

  • Greetings! Very useful advice in this particular post! It is the little changes which will make the biggest changes. Thanks for sharing!

  • I blog often and I truly thank you for your content. This article has truly peaked my interest. I am going to book mark your blog and keep checking for new details about once a week. I opted in for your RSS feed as well.

  • An outstanding share! I have just forwarded this onto a coworker who had been conducting a little research on this. And he actually ordered me dinner simply because I discovered it for him… lol. So let me reword this…. Thank YOU for the meal!! But yeah, thanks for spending the time to talk about this topic here on your web site.

  • You ought to take part in a contest for one of the best sites on the internet. I will recommend this blog!

  • When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get three e-mails with the same comment. Is there any way you can remove people from that service? Thanks!|

  • A fascinating discussion deserves remark. I think that you ought to write extra on this topic, it might not be a taboo subject but typically people are insufficient to talk on such topics. To the next. Cheers

  • I found your blog website on google as well as examine a few of your early blog posts. Remain to maintain the excellent operate. I just extra up your RSS feed to my MSN News Reader. Looking for ahead to learning more from you in the future!?

  • When I originally commented I clicked the -Alert me when brand-new remarks are included- checkbox and now each time a remark is included I obtain four e-mails with the very same remark. Is there any way you can eliminate me from that solution? Thanks!

  • Oh my goodness! a remarkable article guy. Thank you Nonetheless I am experiencing issue with ur rss. Don?t understand why Incapable to subscribe to it. Exists any individual getting similar rss problem? Anybody that knows kindly react. Thnkx

  • Asking questions are truly good thing if you are not understanding something totally, except this post presents good understanding even.|

  • Can I simply say what a relief to find a person that in fact knows what theyre speaking about on the internet. You definitely recognize just how to bring a problem to light and make it essential. More individuals require to read this and also comprehend this side of the story. I angle think youre not extra preferred due to the fact that you absolutely have the present.

  • The other day, while I was at work, my sister stole my iphone and tested to see if it can survive a 30 foot drop, just so she can be a youtube sensation. My iPad is now broken and she has 83 views. I know this is entirely off topic but I had to share it with someone!|

  • There is noticeably a package to find out about this. I think you made sure wonderful factors in attributes also.

  • Excellent web site. Lots of useful info here. I’m sending it to some pals ans also sharing in delicious. And certainly, thank you to your sweat!|

  • What a information of un-ambiguity and preserveness of valuable experience regarding unpredicted feelings.|

  • Area on with this write-up, I truly assume this web site requires much more factor to consider. I?ll most likely be once more to review much more, many thanks for that details.

  • Hi there! I just would like to offer you a huge thumbs up for your excellent info you have here on this post. I am returning to your website for more soon.|