コード例 #1
0
        public async Task OnGetAsync()
        {
            var restaurants = from r in _context.Restaurant select r;

            if (!string.IsNullOrEmpty(searchString))
            {
                searchString = Regex.Replace(searchString, @"[^0-9a-zA-Z]+", "");
                restaurants  = restaurants.Where(r => r.Genre.Contains(searchString) || r.Name.Contains(searchString));
            }
            Restaurant = await restaurants.ToListAsync();

            /*
             * var ratinglist = from r in _context.Rating group r by r.RestaurantID into RestaurantGroup select new { AverageStar = RestaurantGroup.Average(x=> x.Star) };
             * RatingList = new List<double>(await ratinglist.Distinct().ToListAsync());
             */
            foreach (Restaurant r in restaurants)
            {
                Rating = await _context.Rating.Where(rl => rl.RestaurantID == r.RestaurantID).ToListAsync();

                int calculated_rating = 0;
                if (Rating.Count() == 0)
                {
                    // Do Absolutely Nothing
                }
                else
                {
                    foreach (Rating ratings in Rating)
                    {
                        calculated_rating = ratings.Star + calculated_rating;
                    }
                    calculated_rating = Convert.ToInt32(calculated_rating / Rating.Count());
                }
                RatingList.Add(calculated_rating);
            }
        }