public Book[] GetBooksFromGivenBookScores(IEnumerable <string> bookIds) { using (var db = new BooksRecomendationsEntities()) { return(db.Books.Where(x => bookIds.Contains(x.ISBN)).ToArray()); } }
public Book[] GetBooksReadByUser(User user) { using (var db = new BooksRecomendationsEntities()) { return(db.BooksRatings.AsNoTracking().Where(x => x.UserId == user.UserId).Select(z => z.Book).ToArray()); } }
public User[] GetAllUsersWithRatedBooks() { using (var db = new BooksRecomendationsEntities()) { return(db.BooksRatings.AsNoTracking().Select(x => x.User).Distinct().ToArray()); } }
public IEnumerable <string> GetBooksIdsRatedByAtLeastNUsers(int n) { using (var db = new BooksRecomendationsEntities()) { var books = db.BooksRatings.AsNoTracking().GroupBy(y => y.ISBN); return(books.Where(x => x.Count() >= n).OrderByDescending(x => x.Count()).Select(x => x.Key).ToArray()); } }
public string[] GetBooksIdsRatedByAtLeastNUsers(string[] ids, int n) { using (var db = new BooksRecomendationsEntities()) { var books = db.BooksRatings.AsNoTracking().Where(x => ids.Contains(x.ISBN)).GroupBy(y => y.ISBN); return(books.Where(x => x.Count() >= n).Select(x => x.Key).ToArray()); } }
public List <UserSimilar> GetUserNeighbors(int userId, int settingsVersion) { using (var db = new BooksRecomendationsEntities()) { return(db.UserSimilars.AsNoTracking() .Where(x => x.UserId == userId && x.ParametersSet == settingsVersion) .ToList()); } }
public void AddSimilarUsers(List <UsersSimilarity> similarUsers, int settingsVersion) { using (var db = new BooksRecomendationsEntities()) { var users = similarUsers.Select(x => MapToUserSimilarDataModel(x, settingsVersion)).ToList(); db.UserSimilars.AddRange(users); db.SaveChanges(); } }
public void AddTestResult(List <BookScore> score, int settingVersion) { using (var db = new BooksRecomendationsEntities()) { var result = score.Where(x => x.PredictedRate > 0) .Select(bookScore => MapScoreToTest(bookScore, settingVersion)); db.Tests.AddRange(result); db.SaveChanges(); } }
public int[] GetAllUsersWithComputedSimilarity(int settingVersion) { using (var db = new BooksRecomendationsEntities()) { return(db.UserSimilars.AsNoTracking() .Where(x => x.ParametersSet == settingVersion) .Select(x => x.UserId) .Distinct() .ToArray()); } }
public Book[] GetRecommendedBooksForUser(int userId) { using (var db = new BooksRecomendationsEntities()) { return(db.BookRecomendations .Where(x => x.UserId == userId) .OrderByDescending(x => x.PredictedRate) .Select(b => b.Book) .ToArray()); } }
public List <int> GetUsersWhoRatedAnyOfGivenBooks(string[] bookIds) { using (var db = new BooksRecomendationsEntities()) { return(db.BooksRatings.AsNoTracking() .Where(x => bookIds.Contains(x.ISBN)) .Select(x => x.UserId) .Distinct() .ToList()); } }
public double?GetAverageRateForUser(int userId) { using (var db = new BooksRecomendationsEntities()) { var ratings = db.BooksRatings.AsNoTracking().Where(x => x.UserId == userId).ToArray(); if (ratings.Length == 0) { return(null); } var average = ratings.Average(x => x.Rate); return(Math.Round(average, 2)); } }
public BookScore[] GetBooksRatesByUserId(int userId) { using (var db = new BooksRecomendationsEntities()) { return(db.BooksRatings.AsNoTracking() .Where(x => x.UserId == userId) .AsEnumerable() .Select(x => new BookScore { BookId = x.ISBN, Rate = x.Rate, UserId = userId }) .ToArray()); } }
public void SaveParametersSet(Parameter parameter) { using (var db = new BooksRecomendationsEntities()) { var temp = db.Parameters.Find(parameter.Id); if (temp == null) { db.Parameters.Add(parameter); } db.SaveChanges(); } }
public List <int> GetUserIdsWithNorMoreRatedBooks(int n) { using (var db = new BooksRecomendationsEntities()) { var users = db.BooksRatings .AsNoTracking() .Where(x => x.User.BooksRatings.Count >= n && x.User.BooksRatings.Count < 1000) .Select(x => x.UserId) .ToList(); return(users.Distinct() .ToList()); } }
public void AddTestResults(List <BookScore[]> scores, int settingVersion) { using (var db = new BooksRecomendationsEntities()) { var result = new List <Test>(); foreach (var score in scores) { result.AddRange(score.Select(bookScore => MapScoreToTest(bookScore, settingVersion))); } db.Tests.AddRange(result); db.SaveChanges(); } }
public void AddRecommendedBooksForUser(BookScore[] books, int userId) { using (var db = new BooksRecomendationsEntities()) { //delete any if exist db.Database.ExecuteSqlCommand("Delete from BookRecomendation where UserId=@userId", new SqlParameter("@userId", userId)); if (books != null) { db.BookRecomendations.AddRange(books.Select(x => MapToBookRecommendationModel(x, userId))); } db.SaveChanges(); } }
public Parameter GetParameters(int settingsVersion) { using (var db = new BooksRecomendationsEntities()) { var settings = db.Parameters.Where(x => x.Id == settingsVersion).ToList(); return(settings.Select(x => new Parameter { BookPopularity = x.BookPopularity, DistanceSimilarityType = x.DistanceSimilarityType, DistanceType = x.DistanceType, Id = x.Id, Kneigbors = x.Kneigbors, NumberOfBooksEachUserRated = x.NumberOfBooksEachUserRated }) .FirstOrDefault()); } }