这是对.Net C# String.Join how to output “null” instead of empty string if element value is null?的后续问题,其中答案建议使用??
运算符来定义自定义空值,但替换从未被触发。??运算符没有在DataTable DataRow中触发System.DBNull类型
DataSet myDataSet = new DataSet();
mySqlDataAdapter.Fill(myDataSet);
DataTable rotationData = myDataSet.Tables["Table"];
rotationValues = string.Join(", ",
from r in rotationData.Rows.OfType<DataRow>()
select r[5] ?? "null");
当我的代码改变为:
rotationValues = string.Join(", ",
from r in rotationData.Rows.OfType<DataRow>()
select r[5].GetType());
我可以观察到,对于在它们具有有效的数据元素的数据类型是System.Double
而对于其是NULL元素的数据类型为System.DBNull
。 ??
不适用于System.DBNull
?
很好解释,谢谢! –