将 linq 查询结果分配给变量时,可空对象必须有一个值错误
我创建了一个类类型(CustomerInfo)的变量来存储 linq 查询结果,并在查看或使用 colResults 变量时收到错误消息“可空对象必须有一个值”(SurveyDate 是 sql 中的可空字段)表):
Dim var = SurveyDBcontext.QT_Survey_GetSurveySchedule(1, 1, 1).ToList()
Dim colResults = From query In var _
... _
Select New CustomerInfo With { _
.Address = comp.Address, _
.SurveyDate= query.SurveyDate}
如果我将上面的内容更改为:
Dim colResults = From query In var _
... _
Select New With { _
.Address = comp.Address, _
.SurveyDate= query.SurveyDate}
如果工作正常。有什么想法吗?
谢谢。
I've created a variable of a class type (CustomerInfo) to store a linq query result and get the error message 'nullable object must have a value' when I view or use the colResults variable, (SurveyDate is a nullable field in the sql table):
Dim var = SurveyDBcontext.QT_Survey_GetSurveySchedule(1, 1, 1).ToList()
Dim colResults = From query In var _
... _
Select New CustomerInfo With { _
.Address = comp.Address, _
.SurveyDate= query.SurveyDate}
If I change the above to:
Dim colResults = From query In var _
... _
Select New With { _
.Address = comp.Address, _
.SurveyDate= query.SurveyDate}
if works fine. Any ideas?
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的“Address”或“ServeyDate”字段返回空值,并且您的 CustomerInfo 类不允许空值。
第二个示例是生成允许空值的匿名类型。你能检查生成的实际类型吗?猜测第二个示例中的 SurveyDate 是可为空的 DateTime。
Your 'Address' or 'ServeyDate' fields are returning null values and your CustomerInfo class does not allow null values.
The second example is generating an anonymous type which allows nulls. Can you check the actual type that is generated? Guessing SurveyDate is a nullable DateTime in the second example.