public ProjectCostDto DetermineProjectCosts(string code) { ProjectCostDto costs = new ProjectCostDto(); costs.ProjectName = code; ApprovedTimesheetForProject approvedTimesheetSpecification = new ApprovedTimesheetForProject(code); IEnumerable <Timesheet> timesheets = _repository.GetTimesheets(); foreach (Timesheet ts in timesheets.AsQueryable()) { if (approvedTimesheetSpecification.IsSatisfied(ts)) { double rate = _repository.GetRateForTimesheet(ts); if (!costs.weekAlreadyRecorded(ts.WeekStarting.ToShortDateString())) { costs.addWeek(ts.WeekStarting.ToShortDateString()); costs.addCost(ts.CalculateHoursWorkedForProject(code) * (decimal)rate); } else { //Need to find the index in the ICollection to update costs for a given week. int index = costs.Weeks.IndexOf(ts.WeekStarting.ToShortDateString()); decimal existingCosts = costs.Costs.ElementAt(index); existingCosts += ts.CalculateHoursWorkedForProject(code) * (decimal)rate; //Need to update value at a specific index. costs.Costs.RemoveAt(index); costs.Costs.Insert(index, existingCosts); } } } return(costs); }