Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the hueman domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/CloudIngenium.com/htdocs/wp-includes/functions.php on line 6114
How to: Get the Sum of the Values from List in C#.Net? – Knowledge eXchange

How to: Get the Sum of the Values from List in C#.Net?

How to: Get the Sum of the Values from List in C#.Net?

Currently I am working on a new project in which I need to calculate some statistics that involve some basic statistics which require the total amount. The easy way out is just to do a foreach statement and iterate through the list adding the values. But somehow, I wanted something that looked fancier but really cleaner and much easier to maintain and understand. Because of that, I came across the Sum method found in a list. In my case, because I was obtaining all the data from an API and it was returned in a Json… it came as a string. So adding the complexity of adding a list of object’s string properties.


In order to achieve the end result: “Summing all string representations of numbers stored as properties in a list of objects in a clean, easy to manage way” we rely on LINQ:

decimal total = myListOfValues.Sum(x => decimal.Parse(x.NumberAsString));

and just like that, we’ve got our numbers all summed up in just one simple line!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.