public IHttpActionResult GetRecommendedMovies(int RVisitorID) { var moviesBestRatedByUser = principal.MovieRatings .Where(x => x.RVisitorID == RVisitorID).OrderByDescending(x => x.Rating).Take(3).ToList(); if (moviesBestRatedByUser.Count == 0) { return(Ok(new Dictionary <int, float>() { })); } Dictionary <int, dynamic> moviesInfoDict = new Dictionary <int, dynamic>(); foreach (var item in moviesBestRatedByUser) { moviesInfoDict.Add(item.MovieID, new { item.Movies.GenreID, item.Movies.MovieDirectorID, ActorIDs = item.Movies.MovieRoles.Select(x => x.ActorID).ToList(), MinDist = 1.0f / (item.Rating) }); } var Movies = principal.Movies.Where(x => !x.IsDeleted && x.IsCurrent && !moviesInfoDict.Keys.Contains(x.MovieID)).ToList(); Dictionary <int, float> dctMoviesDistance = new Dictionary <int, float>(); Dictionary <string, float> Weights = new Dictionary <string, float> { { "DifferentGenre", 1f }, { "DifferentMovieDirector", 0.5f }, { "ActorsNotPresent", 0.25f } }; foreach (var moviesWatched in moviesInfoDict) { foreach (var moviesToWatch in Movies) { var ActorIDs = principal.MovieRoles.Where(x => x.MovieID == moviesToWatch.MovieID).Select(x => x.ActorID).ToList(); //var AverageRatingList = principal.MovieRatings.Where(x => x.MovieID == moviesToWatch.MovieID).ToList(); //var AverageRating = 0.0; // if(AverageRatingList.Count>0) // AverageRating = AverageRatingList.Average(x=>x.Rating); VectorN objVector = new VectorN(new float[] { Convert.ToInt32((moviesWatched.Value.GenreID != moviesToWatch.GenreID)) * Weights["DifferentGenre"], Convert.ToInt32((moviesWatched.Value.MovieDirectorID != moviesToWatch.MovieDirectorID)) * Weights["DifferentMovieDirector"], Convert.ToInt32((ActorIDs.Intersect((List <int>)moviesWatched.Value.ActorIDs).ToList().Count == 0)) * Weights["ActorsNotPresent"], (float)moviesWatched.Value.MinDist + 1.0f }); var VectorDistance = objVector.GetLength(); if (dctMoviesDistance.Keys.Contains(moviesToWatch.MovieID)) { if (dctMoviesDistance[moviesToWatch.MovieID] > VectorDistance) { dctMoviesDistance[moviesToWatch.MovieID] = VectorDistance; } } else { dctMoviesDistance.Add(moviesToWatch.MovieID, VectorDistance); } } } var dctMoviesDistanceSorted = dctMoviesDistance.OrderBy(x => x.Value).Take(10).ToDictionary(t => t.Key, t => t.Value); return(Ok(dctMoviesDistanceSorted)); }