public int AddAffiliationCategory(AffiliationCategory affiliationCategories) { try { var exist = QueryAffiliationCategories(affiliationCategoriesQuery => affiliationCategoriesQuery.AffiliationFK == affiliationCategories.AffiliationFK && affiliationCategoriesQuery.CategoryFK == affiliationCategories.CategoryFK).ToList(); if (exist.Any()) { if (exist[0].CountOfCategoryPublications != affiliationCategories.CountOfCategoryPublications) { exist[0].CountOfCategoryPublications = affiliationCategories.CountOfCategoryPublications; UpdateAffiliationCategory(exist[0]); } return(exist[0].AffiliationCategoryId); } ; var tracking = _databaseContext.AffiliationCategories.Add(affiliationCategories); _databaseContext.SaveChanges(); return(tracking.Entity.AffiliationCategoryId); } catch (Exception e) { Debug.WriteLine(e.Message); return(0); } }
private void AddValueToAffiliationCategory() { var allPublications = _publicationsRepository.GetPublications(); var affiliationCategories = _affiliationCategoryRepository.GetAffiliationCategories(); var categories = _categoriesRepository.GetCategories(); //var chosenCategories = categories.Where(cat => affiliationCategories.Any(affcat => affcat.CategoriesFK == cat.CategoryId)); foreach (var category in categories) { Debug.WriteLine(category.Name); var pairs = new Dictionary <int, int>(); var queriedPublications = allPublications.Where(pub => pub.CategoriesFK.Contains(category.CategoryId) && pub.AffiliationFK != 0); foreach (var publication in queriedPublications) { if (pairs.Keys.Contains(publication.AffiliationFK)) { pairs[publication.AffiliationFK]++; } else { pairs.Add(publication.AffiliationFK, 1); } } foreach (var pair in pairs) { var affiliationCategory = new AffiliationCategory() { AffiliationFK = pair.Key, CategoryFK = category.CategoryId, CountOfCategoryPublications = pair.Value }; _affiliationCategoryRepository.AddAffiliationCategory(affiliationCategory); } } }
public bool UpdateAffiliationCategory(AffiliationCategory affiliationCategories) { try { var tracking = _databaseContext.AffiliationCategories.Update(affiliationCategories); _databaseContext.SaveChanges(); var isModified = tracking.State == EntityState.Modified; return(isModified); } catch (Exception e) { Debug.WriteLine(e.Message); return(false); } }