对于此 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();
异常信息:
Exception type: InvalidCastException
Exception message: Cannot cast DBNull.Value to type 'System.Int32'. Please use a nullable type.
我不知道可空类型,也不知道如何使用可空类型来克服此异常。
最佳答案
你必须让 int 接受空值 => int?
row.Field<int?>("Presently_Available") == null ? 0 : row.Field<int>("Presently_Available") ;
这是一个link对于可空类型
关于c# - LINQ 查询中的 InvalidCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8803977/