这是 .Net C# String.Join how to output “null” instead of empty string if element value is null? 的后续问题答案建议使用 ??
运算符定义自定义空值,但从未触发替换。
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
进行操作吗?
最佳答案
没有 DBNull
和 null
不是一回事。
但是你可以这样写:
select r[5] == DBNull.Value ? "null" : r[5]);
另一种可能性是使用 Field
扩展方法,它会让您对指定行中的每个列值进行强类型访问,以及 ??
运算符将工作。
select r.Field<string>(5) ?? "null";
关于c# - ??运算符未触发 DataTable DataRow 中的 System.DBNull 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16621962/