The exception "Object cannot be cast from DBNull to other types" is being thrown when pulling data from a DataTable. Obviously the syntax below is not "DBNull aware." Is there a way to prevent this exception from being thrown if a DBNull is found during the casting?
Dim arrayOfDoubles()() As Double = DT.AsEnumerable().Select(Function(x) {Convert.ToDouble(x("Age")), Convert.ToDouble(x("Weight")), Convert.ToDouble(x("Location"))}).ToArray()
In addition, I am actually starting with a string array of column names that I know are double. So is there a way to concatenate all the requested column names together in a query string, and then inject that into the Select statement? In this fashion, I will only obtain a double array for row for which all columns are not DBNull.
If I well understand you need something like this where in
Where
clause you can test your field if there are aDbNull
value.Without being specific in every clause (in where and select) you can use some centralized “trick” to simplify your code as follows: