如何在Asp.Net Mvc中创建一个向用户返回建议列表的方法? [英] How Do I Create A Method In Asp.Net Mvc That Returns A List Of Recommendations To The User?
问题描述
我创建了db和tables以及每个表的模型类,它们具有与列名相同的属性。如何在控制器类[Asp.Net MVC]中创建一个方法,以向用户返回音乐推荐列表,即用户朋友库中的音乐。每个用户的库中都有一个音乐曲目列表,每个用户都有一个朋友列表(用户)。建议是所有音乐曲目,由用户的直接朋友拥有,但不由用户拥有。使用MusicInfoDBContext类和Dbsets类似:public DbSet< Users>用户{get;组;以下是该方法的签名:
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:
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:
recommendations = AllFriendsMusicList.Except(user.MusicList);
推荐答案
您需要从User_Music表中选择所有记录,除了当前Userid的音乐和同一用户的friends表中的所有用户。
假设您正在为用户UserId = 1寻找音乐推荐,那么查询应该像
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
Select * from User_Music
where UserId <> @UserId and UserId in (Select FriendId from Users_Friends where UserId = @UserId)
希望这有帮助!
Hope this helps!
这篇关于如何在Asp.Net Mvc中创建一个向用户返回建议列表的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!