Just do two things to your original code. Remove – remove the wherewhere
constraint, and change the last returnreturn
from return nullreturn null
to return default(T)return default(T)
. This way you can return whatever type you want.
By the way, you can avoid the use of "is"is
by changing your ifif
statement to if (columnValue != DBNull.Value)if (columnValue != DBNull.Value)
.