i have this code and its working but i want it to return dailyTotals list with four values, at the moment it is being returned with three values … i cant seem to figure out how to make it return dailyTotals with four values. BTW the billTotals and dateList are of the same lengh which is currently 7 and they will grow. billTotals and dateList will always remain the same length
List<double> dailyTotals = new List<double>();
string dateValue = "";
double oneDayTotal = 0;
int n = 0;
for (int i = 0; i < billTotals.Count; i += n)
{
dateValue = dateList[i];
oneDayTotal = 0;
while (dateValue == dateList[n])
{
oneDayTotal += billTotals[n];
n++;
}
dailyTotals.Add(oneDayTotal);
}
return dailyTotals;
[EDIT]: Im sorry i should have written this before :/
in the database i have billTotals and the date for each bill stored. so, one date can have multiple bills associated with it. what im trying to do is grab one months data and sum the totals for each day. so the logic i used in the while loop is supposed to sum up the totals while the date is the same … i hope this makes the scenario more clear. 🙂
You never reset n, so you are increasing i by increasingly large amount, skipping over some numbers.
You need to set
n = 0inside the loop.You could also rewrite the code entirely to simplify it and avoid this curious way of incrementing your loop variable. I would suggest creating an array of objects that hold both the date and the total, then you can use LINQ to solve your problem without any complex loops:
You can also use
Zipinstead of creating a separate class, though it will be more readable with the class.