public void UpdateModel(ProjectFirmaModels.Models.Project project, List <ProjectFirmaModels.Models.ProjectFundingSourceExpenditure> currentProjectFundingSourceExpenditures, IList <ProjectFirmaModels.Models.ProjectFundingSourceExpenditure> allProjectFundingSourceExpenditures) { var databaseEntities = HttpRequestStorage.DatabaseEntities; databaseEntities.ProjectExemptReportingYearUpdates.Load(); databaseEntities.ProjectRelevantCostTypes.Load(); var projectFundingSourceExpendituresUpdated = new List <ProjectFirmaModels.Models.ProjectFundingSourceExpenditure>(); if (ProjectFundingSourceExpenditures != null) { // Completely rebuild the list projectFundingSourceExpendituresUpdated = ProjectFundingSourceExpenditures.Where(x => x.IsRelevant).SelectMany(x => x.ToProjectFundingSourceExpendituresSetNullToZero()).ToList(); } currentProjectFundingSourceExpenditures.Merge(projectFundingSourceExpendituresUpdated, allProjectFundingSourceExpenditures, (x, y) => x.ProjectID == y.ProjectID && x.FundingSourceID == y.FundingSourceID && x.CostTypeID == y.CostTypeID && x.CalendarYear == y.CalendarYear, (x, y) => x.ExpenditureAmount = y.ExpenditureAmount, databaseEntities); var currentProjectRelevantCostTypes = project.GetExpendituresRelevantCostTypes(); var allProjectRelevantCostTypes = databaseEntities.AllProjectRelevantCostTypes.Local; var projectRelevantCostTypes = new List <ProjectRelevantCostType>(); if (ProjectRelevantCostTypes != null) { // Completely rebuild the list projectRelevantCostTypes = ProjectRelevantCostTypes.Where(x => x.IsRelevant) .Select(x => new ProjectRelevantCostType(x.ProjectRelevantCostTypeID, x.ProjectID, x.CostTypeID, ProjectRelevantCostTypeGroup.Expenditures.ProjectRelevantCostTypeGroupID)) .ToList(); } currentProjectRelevantCostTypes.Merge(projectRelevantCostTypes, allProjectRelevantCostTypes, (x, y) => x.ProjectID == y.ProjectID && x.CostTypeID == y.CostTypeID && x.ProjectRelevantCostTypeGroupID == y.ProjectRelevantCostTypeGroupID, databaseEntities); project.ExpendituresNote = ProjectFundingSourceExpenditures != null && ProjectFundingSourceExpenditures.Any() ? null : Explanation; }