【发布时间】:2017-07-02 17:06:27
【问题描述】:
我正在对数据表中的数据进行分组,并使用 linq 对一列进行汇总,但是在运行时我收到错误提示
无法将 DBNull.Value 转换为类型 'System.Int32。请使用可空类型”下面是我的代码
Dim resultStyle = invData.Tables(0).AsEnumerable() _
.GroupBy(Function(v) New With {Key .InvestorStyleID = v.Field(Of Integer)("InstitutionalInvestorStyleID"), Key .StyleName = Not IsNothing(v.Field(Of String)("InstitutionalInvestorStyleName"))}) _
.Select(Function(v) New With {Key .InvestorStyleID = v.Key.InvestorStyleID, Key .StyleName = v.Key.StyleName, Key .Sum = v.Sum(Function(r) Double.Parse(r.Item("k001ICGeo").ToString()))})
请建议我如何避免使用空值。我在第二行遇到错误
.GroupBy(Function(v) New With {Key .InvestorStyleID = v.Field(Of Integer)("InstitutionalInvestorStyleID"), Key .StyleName = Not IsNothing(v.Field(Of String)("InstitutionalInvestorStyleName"))}).
我不想取空值。
【问题讨论】:
-
我认为问题出在机构投资者样式 ID。检查这在数据库中是否可以为空。试试
v.Field(Of Integer?)("InstitutionalInvestorStyleID")