The conditional expression type cannot be defined.

I am trying to save a value from a dataset to List here, this is my code

GetPostWRTPager_Class = ds.Tables[0]
  .AsEnumerable()
  .Select(r => new GetPostWRTPager() {
     IsApproved = r["IsApproved"] == DBNull.Value 
       ? DBNull.Value 
       : Convert.ToInt32(r["IsApproved"]),
     ApprovedBy = r["ApprovedBy"],
     ApprovedOn = Convert.ToDateTime(r["ApprovedOn"]), })
  .ToList();

he always gives me the error below

The conditional expression type cannot be determined because there is no implicit conversion between "System.DBNull" and "int"

what is wrong in my code

+4
source share
1 answer

Yes, there is no implicit conversion between DBNulland int. But you can use an extension method Fieldthat supports nullable types (better than using System.Object):

IsApproved = r.Field<int?>("IsApproved"),

If you cannot change this property to make it int?, you need to drop intto object:

IsApproved = r.IsNull("IsApproved") ? DBNull.Value : (object)r.Field<int>("IsApproved"),

int 0 , NULL:

IsApproved = r.Field<int?>("IsApproved").GetValueOrDefault(),

IsApproved bool ( ), 1 true:

IsApproved = r.Field<int?>("IsApproved") == 1,
+2

Source: https://habr.com/ru/post/1678149/


All Articles