private decimal CalculateAverageRate(List <Rates_Users_Places> rates) { var averageRate = 0.00m; if (rates.Count > 0) { var fiveCount = rates.Count(x => x.Rate == 5); var fourCount = rates.Count(x => x.Rate == 4); var threeCount = rates.Count(x => x.Rate == 3); var twoCount = rates.Count(x => x.Rate == 2); var oneCount = rates.Count(x => x.Rate == 1); averageRate = RateCalculator.Calculcate(fiveCount, fourCount, threeCount, twoCount, oneCount); } return(averageRate); }
private void LoadData(string id) { try { var idToNumber = int.Parse(id); using (var db = new PointsOfInterestContext()) { var museum = db.Museums.SingleOrDefault(x => x.Id == idToNumber); var currentUser = db.Users.SingleOrDefault(x => x.Email == this.UserEmail); if (museum == null) { throw new ArgumentNullException("Invalid museum Id"); } else { this.Mus = museum; var ratesMus = db.Rates_Users_Museums .Where(x => x.MuseumId == this.Mus.Id).ToList(); var averageRate = 0.0m; if (ratesMus.Count > 0) { var fiveCount = ratesMus.Count(x => x.Rate == 5); var fourCount = ratesMus.Count(x => x.Rate == 4); var threeCount = ratesMus.Count(x => x.Rate == 3); var twoCount = ratesMus.Count(x => x.Rate == 2); var oneCount = ratesMus.Count(x => x.Rate == 1); averageRate = RateCalculator.Calculcate(fiveCount, fourCount, threeCount, twoCount, oneCount); } museum.AverageRate = averageRate; var currentUserRate = db.Rates_Users_Museums .SingleOrDefault(x => x.MuseumId == this.Mus.Id && x.UserId == currentUser.Id); if (currentUserRate != null) { YourRateLabel.Content = "Your rate is : " + currentUserRate.Rate; } AverageRate.Value = (int)Math.Round(averageRate); MusDes.Content = museum.Descripiton; MusName.Content = museum.MuseumName; var dir = System.IO.Directory.GetParent(Environment.CurrentDirectory).ToString(); var path = System.IO.Path.GetDirectoryName(dir); var imagePath = System.IO.Path.Combine(path + "/Images/Museums/" + museum.ImageUrl); //Uri resourceUri = new Uri(imagePath, UriKind.Relative); BitmapImage bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.UriSource = new Uri(imagePath); bitmap.EndInit(); MusImg.Source = bitmap; } } } catch { } }