ven*_*kat 13 c# linq exception-handling .net-3.5 linq-to-sql
对于这个LINQ查询,我得到以下异常:
(from row in ds.Tables[0].AsEnumerable()
where row.Field<string>("Dept_line_code") == DeptCode &&
row.Field<string>("Skill_Name") == skill &&
row.Field<string>("Acct_Code") == account && row.Field<string>("Location") == dtNewTable.Rows[intRow]["Location"].ToString()
select row.Field<int>("Presently_Available") == null ? 0 : row.Field<int>("Presently_Available")
).FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
例外信息:
异常类型:InvalidCastException
异常消息:无法将DBNull.Value强制转换为"System.Int32".请使用可空类型.
我不知道可空类型,我没有得到如何使用可空类型来克服此异常.
Akr*_*rem 23
你必须使int接受null value => int?
row.Field<int?>("Presently_Available") == null ? 0 : row.Field<int>("Presently_Available") ;
Run Code Online (Sandbox Code Playgroud)
这是Nullable类型的链接
| 归档时间: |
|
| 查看次数: |
16411 次 |
| 最近记录: |