C# de>IEqualityComparer方法有一个优点:您可以为HashSet提供不同的相等比较器,根据域或其他情况定义什么是相等对象……我想您的意思是:public List RatingList=new List();public List Movie
C# de>IEqualityComparer方法有一个优点:您可以为HashSet提供不同的相等比较器,根据域或其他情况定义什么是相等对象……我想您的意思是:public List RatingList=new List();public List Movie,c#,arrays,matrix,C#,Arrays,Matrix,de>IEqualityComparer方法有一个优点:您可以为HashSet提供不同的相等比较器,根据域或其他情况定义什么是相等对象……我想您的意思是:public List RatingList=new List();public List MovieList=新列表();的确,我做到了。固定的! string[] users = new string[5] { "David", "Matt", "Ben", "Chris", "Torri" }; string[] movies = new
de>IEqualityComparer方法有一个优点:您可以为
HashSet
提供不同的相等比较器,根据域或其他情况定义什么是相等对象……我想您的意思是:public List RatingList=new List();public List MovieList=新列表();的确,我做到了。固定的!
string[] users = new string[5] { "David", "Matt", "Ben", "Chris", "Torri" };
string[] movies = new string[4] { "Titanic", "X-men", "Snatch", "Speed"};
ratings = new int[5, 4];
// Note I'm going to use HashSet<T> everywhere because both movies and
// users should be unique in their respective collections
public class User
{
public sealed class UserEqualityComparer : IEqualityComparer<User>
{
public bool Equals(User a, User b)
{
return a != null && b != null && a.Name == b.Name;
}
public int GetHashCode(User some)
{
return some.Name.GetHashCode();
}
}
public string Name { get; set; }
// C# 6 expression bodied properties!!!!
public HashSet<Movie> LikesMovies { get; set; } = new HashSet<Movie>(new Movie.MovieEqualityComparer());
}
public class Movie
{
public sealed class MovieEqualityComparer : IEqualityComparer<Movie>
{
public bool Equals(Movie a, Movie b)
{
return a != null && b != null && a.Name == b.Name;
}
public int GetHashCode(Movie some)
{
return some.Name.GetHashCode();
}
}
public string Name { get; set; }
// C# 6 expression bodied properties!!!!
public HashSet<User> UsersWhoLikeIt { get; set; } = new HashSet<User>(new User.UserEqualityComparer());
}
HashSet<Movie> movies = new HashSet<Movie>(new Movie.MovieEqualityComparer())
{
new Movie { Name = "Star Trek" },
new Movie { Name = "Star Wars" }
};
HashSet<User> users = new HashSet<User>(new User.UserEqualityComparer())
{
new User { Name = "John" },
new User { Name = "Jack" }
};
// Now an user likes a movie:
Movie movie = movies.Single(some => some.Name == "Star Trek");
User user = users.Single(some => some.Name == "John");
// You need to associate both sides of the whole M-N association:
// A movie can be liked by many users and an user can like many movies...
movie.UsersWhoLikeIt.Add(user);
user.LikesMovies.Add(movie);
public class User
{
public string Name { get; set; }
}
public class Movie
{
public string Title { get; set; }
}
public class Rating
{
public User RatingUser { get; set; }
public Movie RatingMovie { get; set; }
}
public List<User> UserList = new List<User>();
public List<Movie> MovieList = new List<Movie>();
public List<Ratings> RatingList = new List<Rating>();
UserList.Add(new User() { Name = "David"} );
UserList.Add(new User() { Name = "Matt"} );
UserList.Add(new User() { Name = "Ben"} );
UserList.Add(new User() { Name = "Chris"} );
UserList.Add(new User() { Name = "Torri"} );
MovieList.Add(new User() { Title = "Titanic"} );
MovieList.Add(new User() { Title = "X-men"} );
MovieList.Add(new User() { Title = "Snatch"} );
MovieList.Add(new User() { Title = "Speed"} );
ratings = new byte[5, 4];
ratingtable:
int userID PK
int movieID PK
byte rating