For this LINQ query I'm getting the exception below:
(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 information:
Exception type: InvalidCastException
Exception message: Cannot cast DBNull.Value to type 'System.Int32'. Please use a nullable type.
I don't know nullable type and I'm not getting how to use nullable type to overcome this exception.
Well, you can just put multiple "where" clauses in directly, but I don't think you want to. Multiple "where" clauses ends up with a more restrictive filter - I think you want a less restrictive one.
The Any operator is used to check whether any element in the sequence or collection satisfy the given condition. If one or more element satisfies the given condition, then it will return true. If any element does not satisfy the given condition, then it will return false.
Use into keyword in LINQ query to form a group or to continue a query after a select clause. In the above query, the 'into' keyword introduced a new range variable teenStudents, so the first range variable s goes out of scope.
select row.Field<int?>("Presently_Available")
you have to make int accept null value => int?
row.Field<int?>("Presently_Available") == null ? 0 : row.Field<int>("Presently_Available") ;
and this is a link for the Nullable Types
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With