帮助了解列表 [英] Help with understanding List
本文介绍了帮助了解列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我希望获得一些帮助,以帮助我理解如何在Movie和Category类构造函数中实例化List,该构造函数将用于Categories和Movies属性.我知道如何创建列表,但我不知道如何以正确的方式对类别进行分类.
公共 class 电影
{
公共 字符串标题{ get ; 设置; }
公共 int YearOfPublication { get ; 设置; }
公共 double 评分{ get ; 设置; }
公共 int NumberOfUserVotes { get ; 设置; }
公共 ICollection< Category>类别{获取; 设置; }
公共 Movie()
{
}
公共 类类别
{
公共 字符串名称{ get ; 设置; }
公共 ICollection< Movie>电影{获取; 设置; }
公共 Category()
{
}
}
}
解决方案
这并不是一个好主意-您将信息存储在两个地方,而且很容易混淆.例如,如果删除一个类别,则必须检查所有电影以确保它们均未引用该类别,而且在删除电影时,也必须检查所有类别.很容易被打结而忘了一些东西.此外,如果将其存储在数据库中,则必须处理数据以找出存储方式-您不能在类别数量可变的电影中使用一列,也不能在类别中使用一列电影的数量可变-因此您需要一个用于存储链接的表.
相反,我将拥有一个LinkMovieToCategories类,并从Movies(使用GetCategories方法)和Categories(使用GetMovies方法)中查询
然后,这些可以从Link类中获取信息.
您好,我重新定义了一些类.现在我对LINQ查询有一个新问题,收到此消息
无法将类型'System.Collections.Generic.List'隐式转换为'System.Collection.Generic.IEnumerable'.显式转换退出(您是否缺少类型转换?)"
有什么建议如何解决这个问题? thx
公共 class 电影 { 公共 字符串标题{ get ; 设置; } 公共 int YearOfPublication { get ; 设置; } 公共 double 评分{ get ; 设置; } 公共 int NumberOfUserVotes { get ; 设置; } 公共 ICollection< category>类别{获取; 设置; } } 公共 类类别 { 公共 字符串名称{ get ; 设置; } 公共 ICollection<电影>电影{获取; 设置; } } List<电影>电影; 列表<类别>类别; 公共 InMemoryMovieService() { 电影= 新列表<电影>(); category = 新列表< category>(); 电影movie_one = 新 Movie(); movie_one.Title = " ; // 遗漏了一些东西 类别cat_one = 新 Category(); cat_one.Name = " ; 电影.添加(movie_one); Categories.Add(cat_one); } 公共 无效 connectMovieAndCategory(电影,类别类别) { Category _category = 新 Category(); 电影_movies = 新 Movie(); _movies.Categories.Add(category); _category.Movies.Add(movie); } 公共 IEnumerable<电影> MoviesFromYear( int 年) { // 不起作用. IEnumerable<电影> movieresult =电影.其中(m = > m.YearOfPublication == year).Select(m = > m.Title).ToList(); // 工作正常 var = movieresult; } </ 电影 > </ 电影 > </ 类别 > 电影 </ > 电影 > < / 电影 > </ 类别 >
Hello I would like some help with understanding how I can instansiate a List in the Movie and Category classes constructors that will be used for the Categories and Movies properties. I know how to create a list but I don''t know how to do it for categories in the right way.
public class Movie { public string Title { get; set; } public int YearOfPublication { get; set; } public double Rating { get; set; } public int NumberOfUserVotes { get; set; } public ICollection<Category> Categories { get; set; } public Movie() { } public class Category { public string Name { get; set; } public ICollection<Movie> Movies { get; set; } public Category() { } } }解决方案That''s not really a good idea - you are storing information in two places, and it is far too easy to get it confused. For example, if you delete a Category, you have to check all Movies to make sure none of them refer to it, but also when you delete a Movie, you have to check all categories. Much, much to easy to get tied up in knots and forget something. In addition, if you store this in a database, you have to play around with your data to work out how to store it - you can''t have a column in movies with a variable number of categories, or a column in categories which has a variable number of movies - so you need a table which stores the links.
Instead, I would have a LinkMovieToCategories class and query that from Movies (with a GetCategories method) and from Categories (with a GetMovies method)
These can then get the info from the Link class.
Hello I redefined the classes some. Now I have a new problem with the LINQ query getting this message
"Cannot implicitly convert type ’System.Collections.Generic.List’ to ‘System.Collection.Generic.IEnumerable’. An explicit conversion exits(are u missing a cast?)"
Any suggestions how to solve this? thx
public class Movie { public string Title { get; set; } public int YearOfPublication { get; set; } public double Rating { get; set; } public int NumberOfUserVotes { get; set; } public ICollection<category> Categories { get; set; } } public class Category { public string Name { get; set; } public ICollection<movie> Movies { get; set; } } List<movie> movies; List<category> categories; public InMemoryMovieService() { movies = new List<movie>(); categories = new List<category>(); Movie movie_one = new Movie(); movie_one.Title = "Lords of the rings"; //left out some stuff Category cat_one = new Category(); cat_one.Name = "Action"; movies.Add(movie_one); categories.Add(cat_one); } public void connectMovieAndCategory(Movie movie, Category category) { Category _category = new Category(); Movie _movies = new Movie(); _movies.Categories.Add(category); _category.Movies.Add(movie); } public IEnumerable<movie> MoviesFromYear(int year) { //Doesn't work. IEnumerable<movie> movieresult = movies.Where(m => m.YearOfPublication ==year).Select(m => m.Title).ToList(); //Works fine var = movieresult; } </movie></movie></category></movie></category></movie></movie></category>
这篇关于帮助了解列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文