You can write your own extension method like this
public static T GetField<T>(this DataRow row,string columnName) { object value = row[columnname]; return (value != DbNull.Value)? (T)value : default(T); }
Then you can use
if (row.GetField<string>(columnname) != record.Forename) {
This extension must support nullable types as well
var value = row.GetField<int?>(columnname);
The only difference from DataRowExtension.Field and our implementation is that it will throw an exception when we try to use DbNull.Value for any value type (except for the NULL type), in which case this method will return the default value.
source share