public TourModel GetNRandomPlaces(int maxTime, int[] timeList, WeatherEntity weather, SubtypeDataEntity subtypeData, bool addRestaurant, string savedPlaces) { List <PlaceEntity> newTourList = new List <PlaceEntity>(); bool addedRestaurant = false; if (savedPlaces != null) { var places = new List <string>(savedPlaces.Split(',')); for (int i = 0; i < places.Count(); i++) { places[i] = places[i].Trim('"', '[', ']'); } foreach (string name in places) { PlaceEntity place = Tour.Where(i => i.Name == name).FirstOrDefault(); if (place != null) { newTourList.Add(place); maxTime -= 45; Tour = Tour.Where(u => u.Name != place.Name).ToList(); } } if (addedRestaurant) { bool found = false; RestaurantEntity restaurant = new RestaurantEntity(); foreach (string name in places) { restaurant = Restaurants.Where(i => i.Name == name).FirstOrDefault(); if (restaurant != null) { found = true; break; } } if (found) { newTourList.Add(restaurant); Restaurants = Restaurants.Where(u => u.Name != restaurant.Name).ToList(); addedRestaurant = true; } } } if (!addedRestaurant) { if (addRestaurant) { double[] restaurantRatings = new double[Restaurants.Count()]; for (int i = 0; i < Restaurants.Count(); i++) { restaurantRatings[i] = double.Parse(Tour.ElementAt(i).Rating, System.Globalization.CultureInfo.InvariantCulture); } int restaurant_index = GetRandomRestaurantIndex(restaurantRatings); newTourList.Add(Restaurants.ElementAt(restaurant_index)); Restaurants = Restaurants.Where(u => u.Name != Restaurants.ElementAt(restaurant_index).Name).ToList(); } } double[] ratings = new double[Tour.Count()]; for (int i = 0; i < Tour.Count(); i++) { if (Tour.ElementAt(i).Rating != null) { ratings[i] = double.Parse(Tour.ElementAt(i).Rating, System.Globalization.CultureInfo.InvariantCulture); } else { ratings[i] = 3; } } List <int> index_list = GetRandomIndexList(ratings, timeList, maxTime, weather, subtypeData); foreach (int i in index_list) { newTourList.Add(Tour.ElementAt(i)); } int[] index_array = index_list.ToArray(); foreach (int i in index_array) { Tour = Tour.Where(u => u.Name != Tour.ElementAt(i).Name).ToList(); for (int j = 0; j < index_array.Length; j++) { if (index_array[j] > i) { index_array[j] -= 1; } } } return(new TourModel(newTourList)); }