public void UpdateRatingForAllPublications() { var publications = Publications .Include(pub => pub.Reviews) .Where(p => p.Reviews.Count != 0); foreach (var publication in publications) { publication.Rating = publication.Reviews.Average(rev => rev.Rating); } Publications.UpdateRange(publications); }
public void UpdateRatingForPublication(int publicationId) { var publication = Publications.Include(pub => pub.Reviews).Where(p => p.Id == publicationId).FirstOrDefault(); if (publication.Reviews.Count > 0) { publication.Rating = publication.Reviews.Average(rev => rev.Rating); } else { publication.Rating = 0; } Publications.Update(publication); }