LINQ to Entities:求和子值
我已经查看了许多有关此错误的报告,但我看到的解决方案要么不适用,要么在我的情况下不起作用(或者我不明白如何使它们起作用?),所以我不情愿地发布此希望寻找答案。
我在 L2E 中提交以下简单查询:
var symbolCover = DataModel.NCoverAnalysisDetail.Where(d => d.IsNew &&
d.NCoverAnalysis.BuildTime > FromNewDate).Sum(c => c.SymbolCoverage);
它抛出以下异常:
转换为值类型“Int32”失败,因为具体化值为 null。结果类型的泛型参数或查询必须使用可为 null 的类型。
表上没有可为空的列,并且发送到数据库的 SQL(见下文)返回 0(零),因此我看不出问题所在。
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'
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'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)