void DisplayRecommendedFilms(Int32 userId)
 {
     try
     {
         clsDataConnection DB = new clsDataConnection();
         DB.AddParameter("@UserId", userId);
         DB.Execute("sproc_tblFilmRecommendation_FilterByUserId");
         Int32 recordCount = DB.Count;
         Int32 index       = 0;
         Int32 filmId      = 0;
         if (recordCount == 0)
         {
             clsDynamicPanel aDynamicPanel = new clsDynamicPanel();
             pnlRecommendations.Controls.Add(aDynamicPanel.GenerateEmptyListPanel("recommended films"));
         }
         else
         {
             while (index < recordCount)
             {
                 filmId = Convert.ToInt32(DB.DataTable.Rows[index]["FilmId"]);
                 pnlRecommendations.Controls.Add(anImdbApi.GetImdbInformation(filmId));
                 index++;
             }
         }
         pnlRecommendations.Visible = true;
     }
     catch
     {
         pnlError.Visible = true;
     }
 }
        void GenerateRecommendations(int genreId)
        {
            //create a new instance of the MLContext class
            MLContext mlContext = new MLContext();

            (IDataView trainingDataView, IDataView testDataView) = LoadData(mlContext);
            ITransformer model = BuildAndTrainModel(mlContext, trainingDataView);

            //create an instance of the class which represents the IDataView/ DataViewRow schema
            DataViewSchema modelSchema;

            //find the saved model
            var path = Server.MapPath(@"~/Model.zip");

            //load the saved model
            ITransformer trainedModel = mlContext.Model.Load(path,
                                                             out modelSchema);

            //create a prediction engine for film recommendations
            var predictionEngine = mlContext.Model.CreatePredictionEngine <clsFilmRating, MovieRatingPrediction>(trainedModel);

            var     tempUserId = Session["UserId"];
            Single  userId;
            Boolean signedIn = false;

            //check if user is signed in
            if (tempUserId == null)
            {
                userId = 1;
            }
            else
            {
                userId   = Convert.ToSingle(tempUserId);
                signedIn = true;
            }

            //get all films from the database
            clsFilmGenreCollection AllFilms = new clsFilmGenreCollection();

            AllFilms.GetAllFilmsByGenre(genreId);

            List <clsFilmPrediction> AllPredictions  = new List <clsFilmPrediction>();
            clsFilmPrediction        aFilmPrediction = new clsFilmPrediction();

            foreach (clsFilmGenre aFilm in AllFilms.AllFilmsByGenre)
            {
                var potentialRecommendation = new clsFilmRating {
                    UserId = userId, FilmId = aFilm.FilmId
                };
                var movieRatingPrediction = predictionEngine.Predict(potentialRecommendation);
                //if a rating is high enough, add it to film predictions
                if (Math.Round(movieRatingPrediction.Score, 1) > 4.4)
                {
                    aFilmPrediction        = new clsFilmPrediction();
                    aFilmPrediction.FilmId = aFilm.FilmId;
                    aFilmPrediction.Score  = movieRatingPrediction.Score;

                    AllPredictions.Add(aFilmPrediction);
                }
            }

            //sort them by score
            AllPredictions.Sort();

            //get the ten best recommendations
            var topTenPredictions = AllPredictions.Take(10);

            clsFilmRecommendationCollection FilmRecommendations  = new clsFilmRecommendationCollection();
            clsFilmRecommendation           aRecommendationToAdd = new clsFilmRecommendation();

            clsMostRecommendedFilmsCollection AllMostRecommendedFilms = new clsMostRecommendedFilmsCollection();
            clsMostRecommendedFilms           aMostRecommendedFilm    = new clsMostRecommendedFilms();

            foreach (clsFilmPrediction aTopTenPrediction in topTenPredictions)
            {
                if (signedIn)
                {
                    //save the recommendations for future use
                    aRecommendationToAdd        = new clsFilmRecommendation();
                    aRecommendationToAdd.FilmId = aTopTenPrediction.FilmId;
                    aRecommendationToAdd.UserId = Convert.ToInt32(userId);
                    FilmRecommendations.ThisFilmRecommendation = aRecommendationToAdd;
                    FilmRecommendations.Add();
                }

                aMostRecommendedFilm = new clsMostRecommendedFilms();
                AllMostRecommendedFilms.ThisMostRecommendedFilm.FilmId = aTopTenPrediction.FilmId;

                if (AllMostRecommendedFilms.ThisMostRecommendedFilm.Find(aTopTenPrediction.FilmId) == true)
                {
                    AllMostRecommendedFilms.IncreaseTimesRecommended();
                }
                else
                {
                    AllMostRecommendedFilms.Add();
                }
                //get imdb information for film
                pnlRecommendations.Controls.Add(anImdbApi.GetImdbInformation(aTopTenPrediction.FilmId));
            }
            pnlRecommendations.Visible = true;

            //save the model for later use
            mlContext.Model.Save(trainedModel, modelSchema, path);
        }