Click here to Skip to main content
15,885,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have created the db and tables and a model classes of each table having the same properties as column name. How do i create a method in controller class [Asp.Net MVC] to return a list of recommendation of music to the user i.e the musics that are in the library of users friends. Each user has a list of music tracks in their library and each user has a list of friends(Users). Recommendations are all the music tracks, which are owned by the user’s immediate friends but not by the user. Similary using a MusicInfoDBContext class and Dbsets: public DbSet<Users> Users{ get; set; } Following is the signature of the method:

C#
List<Music> getMusicRecommendations(User user)
and the DBScript is as follows:


CREATE DATABASE OnlineMusicStore
  CREATE TABLE Users(
    ID int primary key identity(1,1),
    Username nvarchar(255)
    )

    CREATE TABLE Music
    (
    ID int primary key identity(1,1),
    MusicName nvarchar(255),
    )

    CREATE TABLE Users_Music
    (
    UserId int foreign key references Users(ID),
    MusicId int foreign key references Music(ID)
    )

    CREATE TABLE Users_Friends
    (
    UserId int foreign key references Users(ID),
    FriendId int foreign key references Users(ID)
    )
 public class Users
    {
        public int ID { get; set; }
        public string UserName { get; set; }
    }
  public class Music
    {
        public int ID { get; set; }
        public string MusicName { get; set; }
    }
 public class Users_Music
    {
        public int UserId { get; set; }
        public int MusicId { get; set; }
    }
  public class Users_Friends
    {
        public int UserId { get; set; }
        public int FriendId { get; set; }
    }


Can anyone elaborate the solution as i can understand it better. One of my Friend suggested using:

C#
recommendations = AllFriendsMusicList.Except(user.MusicList);
Posted
Updated 10-Oct-15 8:02am
v2

1 solution

You need to select all records from User_Music table except musics for the current Userid and all users that are in the friends table for the same user.

Suppose if you are looking for music recommendations for a user UserId = 1 then the query should be like

SQL
Select * from User_Music
where UserId <> @UserId and UserId in (Select FriendId from Users_Friends where UserId = @UserId)


Hope this helps!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900