2013-06-21 59 views
2

我不能为我的生活出了问题是什么。每次查询执行“ToList()”时,我都会看到上面的错误。铸造价值型“的Int32”失败,因为物化值为null

这里有更多的信息就可以了:

<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; } 
} 
+1

的可能重复的[流延到值类型“的Int32”失败,因为物化值为空](http://stackoverflow.com/questions/6864311/the-cast-to-value-type-in​​t32-failed-因为最物化的价值,是空的) – Rawling

回答

8

我猜这个问题就在这里:

ReleaseYear = (int) shows.ReleaseYear 

你为什么要投shows.ReleaseYearint?是因为它还不是int?也许它实际上是一个Nullable<int>

int不能持有null值,错误是告诉你,这是遇到的数据null价值,让价值不能被强制转换为int

你要么需要改变你的数据,不允许null值该字段,或更改你的类型的Nullable<int>(或int?的简称)。

+1

+1提空类型。 – Brian

+0

谢谢。我所做的只是添加“?”将Shows类转换为此变量。 –

6

它会告诉你正确的有:

演员阵容价值型 '的Int32' 失败,因为物化值为null

这是有问题的行:

ReleaseYear = (int) shows.ReleaseYear, 

检查您的数据并确保一切有ReleaseYear,或改用int?

2

如果您收到空值,然后尝试使用铸造这些方法 -

Convert.ToInt32(串),如果用于解析值为null这将返回0。

Int32.TryParse(字符串,OUT INT),这将返回true/false,如果它不能解析分别/。

在程序耗时解析值之前,检查它是否有效。

相关问题