这是一个后续问题.Net C# String.Join 如果元素值为 null,如何输出“null”而不是空字符串? https://stackoverflow.com/questions/16621347/net-c-sharp-string-join-how-to-output-null-instead-of-empty-string-if-element答案建议使用??
运算符定义自定义空值,但从未触发替换。
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
. Does ??
不进行操作System.DBNull
?
No DBNull
and null
不是同一件事。
但你可以这样写:
select r[5] == DBNull.Value ? "null" : r[5]);
另一种可能性是使用Field
扩展方法,它将为您提供对指定行中每个列值的强类型访问,以及??
操作员将工作。
select r.Field<string>(5) ?? "null";
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)