为什么三元运算符显示错误? [英] Why ternary operator showing error ?
本文介绍了为什么三元运算符显示错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
dr[columnname] = datavalue.Trim() == "" ? DBNull.Value : Convert.ToInt32(datavalue);
什么是正确的编码?
如果datavalue.Trim()==",那么我想将空格传递到我的列dr [columnname].
what is the correct coding?
if datavalue.Trim() == "" then i want to pass the blank space to my column dr[columnname].
推荐答案
您正在尝试:
请参见以下代码:
You are tring impossible:
See the below code:
DataTable table = new DataTable();
Nullable<int> X = 10;
X = null;
table.Columns.Add("Dosage", X.GetType() );
string datavalue = "123";
DataRow row = table.NewRow();
row["Dosage"] = datavalue.Trim() == "" ? 0 : Convert.ToInt32(datavalue);
在此值中使用0而不是0,如果您尝试使用null或DBNull或应用程序无法正常运行的任何内容,则应考虑使用0代替
In this instead of 0, if you try null or DBNull or anything your application will not work, you should considering 0 instead
您正在表达式中混合类型, ^ ]不是Int32
.
You are mixing types in your expression,DBNull.Value
[^] is not anInt32
.
尝试一下,
Try this,
dr[columnname] = datavalue.Trim() == "" ? String.Empty : Convert.ToInt32(datavalue).ToString();
或
Or
dr[columnname] = datavalue.Trim() == "" ? (int?)null : Convert.ToInt32(datavalue);
这篇关于为什么三元运算符显示错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文