Saturday, March 24, 2012

SUM LIST By Month and Year - HELP PLEASE!


{

I need to loop through a list, find the amounts for the same month and year. How can I find all the values for a month and year, add them and set them to an new variable?

Below is the codle of the list I am using to Loop and its results, thank you for your help!

Response.Write("<table border=1 class=tdRowCenter width=300>");foreach(GenericList AmonuntsLoopin MyList)

Response.Write(<SPAN class=st>"&lt;tr><td>Month</td><td>"</SPAN> + AmonuntsLoop.Month + <SPAN class=st>"&lt;/td><td>Year</td><td>"</SPAN> + AmonuntsLoop.Year + <SPAN class=st>"&lt;/td><td>Amount</td><td>"</SPAN> + AmonuntsLoop.MonthlyAmount + <SPAN class=st>"&lt;/td></tr>"</SPAN>);
}

Response.Write(<SPAN class=st>"&lt;/table>"</SPAN>);

 

output for testing only

Month1Year2006Amount17722.94Month2Year2006Amount18093.19Month3Year2006Amount18157.50Month4Year2006Amount16371.90Month5Year2006Amount17338.10Month6Year2006Amount17345.06Month7Year2006Amount18921.64Month8Year2006Amount18836.68Month9Year2006Amount20741.74Month10Year2006Amount17362.87Month11Year2006Amount15228.85Month1Year2005Amount17138.79Month2Year2005Amount16802.48Month3Year2005Amount18000.43Month4Year2005Amount16040.48Month5Year2005Amount9755.48Month6Year2005Amount19097.73Month7Year2005Amount18267.06Month8Year2005Amount18832.51Month9Year2005Amount20163.64Month10Year2005Amount16937.13Month11Year2005Amount16114.66Month12Year2005Amount18602.90Month1Year2006Amount22209.40Month2Year2006Amount20738.59Month3Year2006Amount18499.77Month4Year2006Amount21951.75Month5Year2006Amount19402.30Month6Year2006Amount21563.31Month7Year2006Amount22855.29Month8Year2006Amount26106.09Month9Year2006Amount22854.72Month10Year2006Amount25766.62Month11Year2006Amount19548.18Month1Year2005Amount20765.76Month2Year2005Amount20233.43Month3Year2005Amount19161.78Month4Year2005Amount20528.22Month5Year2005Amount18855.83Month6Year2005Amount20129.81Month7Year2005Amount21382.53Month8Year2005Amount25826.96Month9Year2005Amount22054.56Month10Year2005Amount25469.49Month11Year2005Amount22037.02Month12Year2005Amount21151.57

For example I need to add 1/2005 entries and end up with39,932.34

This is pretty crude, but you could do something like this...

1string curMonth ="";2string curYear ="";34double totalMonth = 0;5double totalYear = 0;67foreach(GenericList AmountsLoopin MyList)8{9// suggestion on writing your html line10 Response.Write(String.Format("<tr><td>{0}</td></tr>", AmountsLoop.Month)11 totalMonth += AmountsLoop.MonthlyAmount;12 totalYear += AmountsLoop.MonthlyAmount;1314if (AmonuntsLoop.Month != curMonth)15 {16 Response.Write(String.Format("{0}'s total is {1:c}", curMonth, totalMonth));17 curMonth = AmonuntsLoop.Month;18 totalMonth = 0;19 }2021if (AmonuntsLoop.Year != curYear)22 {23 Response.Write(String.Format("{0}'s total is {1:c}", curYear, totalYear));24 curYear = AmonuntsLoop.Year;25 totalYear = 0;26 }27}28

Thanks, but is there another non crude way using Generics? I have the data in a Generic List

0 comments:

Post a Comment