我的代码中有以下行为数据表列赋值。它可以为空或价格。
double? Price;
price = range.Cells[i, j].Value2 == null ? Double.NaN : range.Cells[i,j].Value2;
row["Price"] = **(price != Double.NaN) ? price.Value** : (object)DBNull.Value;
VS 2010 生成一条绿色波浪线(星对内的代码)说
The expression type is always false since double can never be null
我怎样才能解决这个问题,以便在需要时为数据库表中的价格分配 null?
请您参考如下方法:
使用 Double.NaN 对您没有帮助,因为 NaN 甚至不等于自身。目前尚不清楚为什么要分两个阶段执行此操作 - 我建议:
row["Price"] = range.Cells[i, j].Value2 ?? (object) DBNull.Value;
