public void UpdateModel(ProjectFirmaModels.Models.Project project, DatabaseEntities databaseEntities) { var currentProjectFundingSourceBudgets = project.ProjectFundingSourceBudgets.ToList(); databaseEntities.ProjectFundingSourceBudgets.Load(); var allProjectFundingSourceBudgets = databaseEntities.AllProjectFundingSourceBudgets.Local; var currentProjectNoFundingSourceIdentifieds = project.ProjectNoFundingSourceIdentifieds.ToList(); databaseEntities.ProjectNoFundingSourceIdentifieds.Load(); var allProjectNoFundingSourceIdentifieds = HttpRequestStorage.DatabaseEntities.AllProjectNoFundingSourceIdentifieds.Local; project.FundingTypeID = FundingTypeID; var projectFundingSourceBudgetsUpdated = new List <ProjectFirmaModels.Models.ProjectFundingSourceBudget>(); if (ProjectFundingSourceBudgets != null) { // Completely rebuild the list projectFundingSourceBudgetsUpdated = ProjectFundingSourceBudgets.Where(x => x.IsRelevant ?? false).SelectMany(x => x.ToProjectFundingSourceBudgets()).ToList(); } currentProjectFundingSourceBudgets.Merge(projectFundingSourceBudgetsUpdated, allProjectFundingSourceBudgets, (x, y) => x.ProjectID == y.ProjectID && x.FundingSourceID == y.FundingSourceID && x.CostTypeID == y.CostTypeID && x.CalendarYear == y.CalendarYear, (x, y) => x.SetSecuredAndTargetedAmounts(y.SecuredAmount, y.TargetedAmount), databaseEntities); var projectNoFundingSourceAmountsUpdated = new List <ProjectNoFundingSourceIdentified>(); if (FundingTypeID == FundingType.BudgetSameEachYear.FundingTypeID && NoFundingSourceIdentifiedYet != null) { // Completely rebuild the list projectNoFundingSourceAmountsUpdated.Add(new ProjectNoFundingSourceIdentified(project.ProjectID) { NoFundingSourceIdentifiedYet = NoFundingSourceIdentifiedYet }); } else if (FundingTypeID == FundingType.BudgetVariesByYear.FundingTypeID && NoFundingSourceAmounts != null) { // Completely rebuild the list projectNoFundingSourceAmountsUpdated = NoFundingSourceAmounts.Where(x => x.MonetaryAmount.HasValue) .Select(x => new ProjectNoFundingSourceIdentified(project.ProjectID) { CalendarYear = x.CalendarYear, NoFundingSourceIdentifiedYet = x.MonetaryAmount.Value }) .ToList(); } // set if funding type is "Varies By Year", delete rows otherwise currentProjectNoFundingSourceIdentifieds.Merge(projectNoFundingSourceAmountsUpdated, allProjectNoFundingSourceIdentifieds, (x, y) => x.ProjectID == y.ProjectID && x.CalendarYear == y.CalendarYear, (x, y) => x.NoFundingSourceIdentifiedYet = y.NoFundingSourceIdentifiedYet, databaseEntities); var currentProjectRelevantCostTypes = project.GetBudgetsRelevantCostTypes(); 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.Budgets.ProjectRelevantCostTypeGroupID)) .ToList(); } currentProjectRelevantCostTypes.Merge(projectRelevantCostTypes, allProjectRelevantCostTypes, (x, y) => x.ProjectID == y.ProjectID && x.CostTypeID == y.CostTypeID && x.ProjectRelevantCostTypeGroupID == y.ProjectRelevantCostTypeGroupID, databaseEntities); }