我有这个基类,我创建了一个Media(List)列表
public abstract class Media
{
#region Fields
private DateTime? _releaseDate;
#endregion
#region Constructor
public Media()
{
}
#endregion
#region Properties
//Used to convert the Json propertyname
[Key]
public int MediaId { get; set; }
[JsonProperty(PropertyName = "id")]
public int ApiId { get; set; }
[JsonProperty(PropertyName = "title")]
[MaxLength(200), MinLength(0)]
public virtual string Title { get; set; }
[JsonProperty(PropertyName = "overview")]
[MaxLength(10000), MinLength(0)]
public string Plot { get; set; }
//Todo definer lengths
[JsonProperty(PropertyName = "poster_path")]
[MaxLength(200), MinLength(0)]
public string Image { get; set; }
[JsonProperty(PropertyName = "status")]
[MaxLength(30), MinLength(0)]
public string Status { get; set; }
[JsonProperty(PropertyName = "release_date")]
public virtual DateTime? ReleaseDate
{
get { return _releaseDate; }
set
{
_releaseDate = value;
}
}
public int TotaltRating { get; set; }
public int AmountOfRaters { get; set; }
public double CurrentRating { get; set; }
[JsonProperty(PropertyName = "genres")]
public virtual List<Genre> GenresInMedia { get; set; }
public virtual List<Actor> ActorsInMedia { get; set; }
public virtual List<Tag> TagsInMedia { get; set; }
public virtual List<Reviewer> SeenBy { get; set; }
#endregion
}
然后我有这个子类(我还有一些其他的子类,但是没有在这里粘贴它们)
public class Movie : Media
{
[JsonProperty(PropertyName = "budget")]
public int Budget { get; set; }
[JsonProperty(PropertyName = "tagline")]
[MaxLength(1000), MinLength(0)]
public string Tagline { get; set; }
[NotMapped]
public List<Trailer> Trailers { get; set; }
}
这是我试图实现的代码。
List<Media> MediaList = new List<Media>();
List<Movie> MovieList = new List<Movie>();
MovieList.Add(new Movie());
MovieList.Add(new Movie());
MovieList.Add(new Movie());
MediaList.AddRange(MovieList);
这给了我一个NullReferenceException
MediaList.AddRange(MovieList.Cast<Media>().ToList());
这也给了我一个NullReferenceException ..
如何将电影列表转换为媒体列表?它似乎不起作用..
提前致谢。
答案 0 :(得分:1)
必须有其他事情发生。我复制了你的代码并运行了这个......
public abstract class Media
{
#region Fields
private DateTime? _releaseDate;
#endregion
#region Constructor
public Media()
{
}
#endregion
#region Properties
//Used to convert the Json propertyname
[Key]
public int MediaId { get; set; }
[JsonProperty(PropertyName = "id")]
public int ApiId { get; set; }
[JsonProperty(PropertyName = "title")]
[MaxLength(200), MinLength(0)]
public virtual string Title { get; set; }
[JsonProperty(PropertyName = "overview")]
[MaxLength(10000), MinLength(0)]
public string Plot { get; set; }
#endregion
}
public class Movie : Media
{
[JsonProperty(PropertyName = "budget")]
public int Budget { get; set; }
[JsonProperty(PropertyName = "tagline")]
[MaxLength(1000), MinLength(0)]
public string Tagline { get; set; }
//[NotMapped]
//public List<Trailer> Trailers { get; set; }
}
然后
List<Media> MediaList = new List<Media>();
List<Movie> MovieList = new List<Movie>();
MovieList.Add(new Movie());
MovieList.Add(new Movie());
MovieList.Add(new Movie());
MediaList.AddRange(MovieList);
MediaList.AddRange(MovieList.Cast<Media>().ToList());
工作得很好。 MediaList中有6个元素。
我不得不评论Trailer,因为你没有提供。