C# List of objects, how do I get the sum of a property

I have a list of objects. One property of the individual object entry is amount. How do I get the sum of amount?

If my list was of type double I may be able to do something like this:

double total = myList.Sum();

However I want to something similar to this, yet this syntax is incorrect.

double total = myList.amount.Sum();

How should I go about accomplishing this? I would love to use the Sum function if possible instead of looping through and calculating the value.

Answers


double total = myList.Sum(item => item.Amount);

Need Your Help

Google Map does not appear in IE7/8 (but works in IE9)

javascript google-maps google-maps-api-3 geoxml3

I am using Google Maps API 3 to show a google map of Canada and a bunch of markers, but in IE7/8, it doesn't show up at all and just gives me a grey rectangle. It loads fine in Firefox/Chrome/Opera...

How to store searchable arrays in MySQL

php mysql arrays

So I've got this form with an array of checkboxes to search for an event. When you create an event, you choose one or more of the checkboxes and then the event gets created with these "attributes"....

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.