Reputation: 6035
I have reviewed numerous reports of this error but the solutions I have seen either do not apply or don't work in my situation (or I'm not understanding how to make them work?) so I'm reluctantly posting this in the hope of finding an answer.
I'm submitting the following simple query in L2E:
var symbolCover = DataModel.NCoverAnalysisDetail.Where(d => d.IsNew &&
d.NCoverAnalysis.BuildTime > FromNewDate).Sum(c => c.SymbolCoverage);
and it throws the following exception:
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.
There are no nullable columns on the tables and the SQL (see below) being sent to the database returns 0 (zero), so I can't see what the problem is.
exec sp_executesql N'SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
SUM([Extent1].[SymbolCoverage]) AS [A1]
FROM [dbo].[NCoverAnalysisDetail] AS [Extent1]
INNER JOIN [dbo].[NCoverAnalysis] AS [Extent2] ON [Extent1].[NCoverAnalysisID] = [Extent2].[ID]
WHERE ([Extent1].[IsNew] = 1) AND ([Extent2].[BuildTime] > @p__linq__0)
) AS [GroupBy1]',N'@p__linq__0 datetime',@p__linq__0='2012-02-17 00:00:00'
Upvotes: 1
Views: 311
Reputation: 15663
var symbolCover = DataModel.NCoverAnalysisDetail
.Where(d => d.IsNew && d.NCoverAnalysis.BuildTime > FromNewDate)
.Sum(c => (int?)c.SymbolCoverage).GetValueOrDefault(0);
Upvotes: 3