private void UpdateFormulaDetailTaxonomies(string[] selectedTaxonomies, FormulaDetail formulaDetailToUpdate) { if (selectedTaxonomies == null) { formulaDetailToUpdate.Taxonomies = new List<Taxonomy>(); return; } var selectedCoursesHS = new HashSet<string>(selectedTaxonomies); var instructorCourses = new HashSet<int> (formulaDetailToUpdate.Taxonomies.Select(c => c.TaxonomyID)); foreach (var taxonomy in db.Taxonomies) { if (selectedCoursesHS.Contains(taxonomy.TaxonomyID.ToString())) { if (!instructorCourses.Contains(taxonomy.TaxonomyID)) { formulaDetailToUpdate.Taxonomies.Add(taxonomy); } } else { if (instructorCourses.Contains(taxonomy.TaxonomyID)) { formulaDetailToUpdate.Taxonomies.Remove(taxonomy); } } } }
private void PopulateAssignedFormulaDetailData(FormulaDetail formulaDetail) { var allTaxonomies = db.Taxonomies; var formulaDetailTaxonomies = new HashSet<int>(); if (formulaDetail != null) formulaDetailTaxonomies = new HashSet<int>(formulaDetail.Taxonomies.Select(c => c.TaxonomyID)); var viewModel = new List<AssignedFormulaDetailData>(); foreach (var taxonomy in allTaxonomies) { viewModel.Add(new AssignedFormulaDetailData { TaxonomyID = taxonomy.TaxonomyID, TaxonomyName = taxonomy.Name, Assigned = formulaDetailTaxonomies.Contains(taxonomy.TaxonomyID) }); } ViewBag.Taxonomies = viewModel; }