问题描述
假设在C#中有一个DataTable dt
类型的double
,其中列"x","y","z"
。
在将值填充到dt
中时,确实会发生这样的情况:仅分配了两个维度,而被分配的这三个维度有意使第三个空缺为DBNull.Value
。
使用根据Transact-SQL syntax的过滤器来计算维度的平均值时,工作正常:
X = (double)dt.Compute("Avg([x])","x IS NOT NULL");
但是,以下结果在System.InvalidCastException: 'Specified cast is not valid.'
sx = (double)dt.Compute("StDev([x])","x IS NOT NULL");
与a seperate method to calculate the standard deviation 相比,这将非常方便。 而且即使在这一点上,converting a column with null values into a List:
List<double> xValues = dt.Rows.OfType<DataRow>().Select(dr => dt.Field<double>("x")).ToList();
System.InvalidCastException: 'Cannot cast DBNull.Value to type 'System.Double'. Please use a nullable type.'
任何使用null-conditionals处理此情况的建议,或有关过滤器的建议,都受到高度赞赏。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)