Thursday, November 6, 2014

The cast to value type 'Double' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type


The cast to value type 'Double' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type OR 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 Solution: Put conversion on values before putting it in functions like Avg, Sum etc like: In below code we have put conversion to decimal in average function.
var res =(from profile in dbModel.ProfileMasters
                                         select new
                                         {
                                             ProfileID = profile.ProfileId,
                                             Profile = profile.ClientName,
                                             Tasks= profile.TaskMasters.Count(),
                                             AverageTime = profile.TaskMasters.Select(x => EntityFunctions.DiffMinutes(x.StartTime, x.EndTime)).Average(y => (decimal?)y.Value),
                                             LastTime = profile.TaskMasters.Max(x=>x.StartTime)
                                         }).ToList();

Share This!


No comments:

Powered By Blogger · Designed By Seo Blogger Templates