c# - Is there a neat way to compare nullable and dbnullable objects? -


i have code this:

foreach (datarow row in datatable.rows) {         if ((string)row["forename"] != record.forename)         {               //         } } 

works great if row["forename"] null in database, dbnull here , can't cast dbnull string, or perform comparison between dbnull , string. values nullable<int>, , can't compare dbnull int?

is there helper method let me nice comparisons or have write extension method myself?

you can use datarow.field extension method supports nullable types:

foreach (datarow row in datatable.rows) {     int? id = row.field<int?>("id");     if(id.hasvalue)     {         console.write("id: " + id.value);     } } 

since string reference type null default. can use datarow.isnull instead check whether dbnull.value or not:

foreach (datarow row in datatable.rows) {     if(row.isnull("forename"))     {         // ...     }      else     {         string forename = row.field<string>("forename");         console.write("forename: " + forename);     } } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -