值类型转换为'Int32'失败,因为实例化的值为空。

3

我真的想不出问题出在哪里。每次查询执行 "ToList()" 时,都会出现上述错误。

以下是更多相关信息:

<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
The cast to value type 'Int32' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.
</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace>
at System.Data.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal) at System.Data.Common.Internal.Materialization.Shaper.GetColumnValueWithErrorHandling[TColumn](Int32 ordinal) at lambda_method(Closure , Shaper ) at System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper) at System.Data.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext() at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at TVDataWebAPI.Controllers.ETSShowsController.GetETSShows(String title, String episodeTitle, String genre, String showTypeDescription, String directorName, String releaseYear, String seasonEpisode) in c:\Users\rarbex\Documents\Visual Studio 2012\Projects\TVDataWebAPI\TVDataWebAPI\Controllers\ETSShowsController.cs:line 83 at lambda_method(Closure , Object , Object[] ) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass13.<GetExecutor>b__c(Object instance, Object[] methodParameters) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.<>c__DisplayClass5.<ExecuteAsync>b__4() at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)
</StackTrace>
</Error>   

public IEnumerable<ETSShows> GetETSShows(string title = null,
{
   string episodeTitle = null, string genre = null,
   string showTypeDescription = null,
   string directorName = null,
   string releaseYear = null,
   string seasonEpisode = null)
   {
      IQueryable<ETSShows> query = from shows in db.ETS_Shows
      from episodes in db.ETS_Episodes.Where(v => v.ShowId == shows.ShowId).DefaultIfEmpty()
      from genres in db.ETS_LKP_Genres.Where(s => s.GenreCode == shows.GenreCode).DefaultIfEmpty()
      from showTypes in db.ETS_LKP_ShowTypes.Where(z => z.ShowTypeCode == shows.ShowTypeCode).DefaultIfEmpty()
      from directors in db.ETS_Directors.Where(p => p.ShowId == shows.ShowId).DefaultIfEmpty()
      select new ETSShows
      {
         dataSource = "ETS",
         Title = shows.Title,
         EpisodeId = episodes.EpisodeId,
         EpisodeTitle = episodes.EpisodeTitle,
         GenreDescription = genres.GenreDescription,
         ShowTypeDescription = showTypes.ShowTypeDescription,
         Name = directors.Name,
         ReleaseYear = (int) shows.ReleaseYear,
         SeasonEpisode = episodes.SeasonEpisode,
         ShowId = shows.ShowId
      };
   }
}

public class ETSShows
{
   public string dataSource { get; set; }
   public string Title { get; set; }
   public int EpisodeId { get; set; }
   public string EpisodeTitle { get; set; }
   public string GenreDescription { get; set; }
   public string ShowTypeDescription { get; set; }
   public string Name { get; set; }
   public int ReleaseYear { get; set; }
   public string SeasonEpisode { get; set; }
   public int ShowId { get; set; }
}

3个回答

9
我猜问题就在这里:

ReleaseYear = (int) shows.ReleaseYear

为什么需要将 shows.ReleaseYear 转换为 int?是因为它不是 int 吗?也许它实际上是一个 Nullable<int>int 无法保存 null 值,错误提示告诉你数据中遇到了 null 值,因此该值无法转换为 int
你需要将该字段的数据更改为不允许 null 值,或者将类型更改为 Nullable<int>(或简写为 int?)。

谢谢。我所做的就是在Shows类中将这个变量加上“?”。 - JJ.

7

它在这里告诉你:

值类型“Int32”的转换失败是因为实体化的值为空

这就是有问题的那一行:

ReleaseYear = (int) shows.ReleaseYear,

请检查您的数据并确保每个数据都有一个“ReleaseYear”属性,或者切换到使用int?


3

如果您得到了null值,尝试使用以下方法进行转换 -

Convert.ToInt32(string),如果用于解析的值为null,则返回0。

Int32.TryParse(string, out int),当它能够解析/不能解析时,它将返回true/false。

在您的程序中,在使用解析后的值之前,请检查其是否有效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接