Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys,

I need to create a method to add a list of entries in a list (eg. I need to add all the incomes of a list to get a total)

List creation and print below, I need to add the total of all income at the bottom:

C#
List<TaxRecord> taxrecords = new List<TaxRecord>()
        {
            new TaxRecord(53664,"John Smith","34 Johns St Sydney",true,6364664.99),
            new TaxRecord(53667,"Steve Henry","38 Victoria Rd Bulli",false,900.88),
            new TaxRecord(53656,"James Tee","65 True St Woonona",true,15678.00)
        };
        foreach (TaxRecord t in taxrecords)
        {
            t.Print();

        }


Posted
Comments
[no name] 5-Oct-15 1:45am    
What problem you are facing here?
Maciej Los 5-Oct-15 1:56am    
-- posted by mistake --

C#
double total = taxrecords.Sum(item => item.Amount);

-KR
 
Share this answer
 
Suppose that your TaxRecord class has a property named income which is of type double then try the below code which will sum up all the incomes within the list.
C#
double total = 0;
foreach (TaxRecord t in taxrecords)
{
    total += t.income;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900