I have an SQL database that I'm extracting data out of. However, some of that data isn't measured and is therefore NULL. I've been able to avoid some null values using this method:
if (dataItem.Feature1!= null) {
trainingCategorial.Feature1= (float)dataItem.Feature1;
}
else {
trainingCategorial.Feature1= float.NaN;
}
Where dataItem is the my dataset which I'm iterating through with foreach.
foreach (dataItem in dataset)
However, now I have the same issue but with an equation:
if(dataItem.CalculateFeature2(afdKenm, (decimal)dataItem.Feature3) != null)
{
trainingCategorial.Feature2 =
dataItem.CalculateFeature2(afdKenm, (decimal)dataItem.Feature3) > 0;
}
The goal of the code is to create a boolean of Feature2. I've checked and it didn't seem as feature3 has any NULL values. My question lies in the way that my c# code doesn't even want to check the dataItem.CalculateFeatures2 before giving the error. It just immediately says that "Nullable object must have a value".
Can anyone help me with this? Thank you in advance!
Feature3? Why are you doing this(decimal)cast? Also the compiler, when it looks at your code it doesn't know what valueFeature3holds. How is it supposed to know what is in the dataset? You declared the field as nullable, and therefore it won't allow you to freely deal with it. Its a safety mechanism. So the real question you should ask yourself is: what is the intended behaviour ifFeature3actually is null? And then implement it. If there is no way forFeature3to ever be null, then you might consider redaclaring the field/property as not nullable.dataItem.CalculateFeature2(..)twice. I would store the result of the first execution in a variable and test/use that result - this may save some (potentially expensive) execution time