Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#处理空值_C#_Null_Nullable - Fatal编程技术网

C#处理空值

C#处理空值,c#,null,nullable,C#,Null,Nullable,我已将CustomerID声明为 int? CustomerID=null; 我在读取DataReader Id = reader["CustomerId"] is DBNull ? null :Convert.ToInt32(reader["CustomerID"]); 这是投掷 Type of conditional expression cannot be determined because there is no implicit conversion between '&

我已将CustomerID声明为

int? CustomerID=null;
我在读取
DataReader

Id = reader["CustomerId"] is DBNull ? null :Convert.ToInt32(reader["CustomerID"]);
这是投掷

 Type of conditional expression cannot be determined because there 
  is no implicit conversion between '<null>' and 'int'
无法确定条件表达式的类型,因为存在 “”和“int”之间没有隐式转换
转换有什么问题?

将您的条件更改为

  reader["CustomerId"] ==  DBNull.Value

把你的条件改为

  reader["CustomerId"] ==  DBNull.Value

A?:条件表达式不能在true和false条件下计算为两种不同的类型。我认为cast
(int?)null
应该可以工作。

a?:条件表达式不能在true和false条件下计算为两种不同的类型。我认为强制转换
(int?)null应该可以工作。

我认为您需要这样做:

if(! reader.IsDBNull(reader.GetOrdinal("CustomerId"))
{
   Id = Convert.ToInt32(reader["CustomerID"]);
}
else
{
   Id = NULL;
}

您需要在读卡器上使用
.IsDBNull
方法提前确定列是否为空-如果为空,甚至不要从读卡器读取值。

我认为您需要这样做:

if(! reader.IsDBNull(reader.GetOrdinal("CustomerId"))
{
   Id = Convert.ToInt32(reader["CustomerID"]);
}
else
{
   Id = NULL;
}
您需要在读卡器上使用
.IsDBNull
方法提前确定列是否为空-如果为空,甚至不要从读卡器读取值。

问题(假设
Id
正确声明)在于条件语句根据真实结果推断结果类型。在您的情况下,该类型为
null
。然后,它将尝试将第二个类型转换为与第一个相同的类型…并且不会从
int
转换为
null

解决方案是将true表达式强制转换为所需的类型:

Id = reader["CustomerId"] == DBNull.Value ?
    (int?) null :
    Convert.ToInt32(reader["CustomerID"]);
问题(假设正确声明了
Id
)是条件语句根据真实结果推断结果类型。在您的情况下,该类型为
null
。然后,它将尝试将第二个类型转换为与第一个相同的类型…并且不会从
int
转换为
null

解决方案是将true表达式强制转换为所需的类型:

Id = reader["CustomerId"] == DBNull.Value ?
    (int?) null :
    Convert.ToInt32(reader["CustomerID"]);

发生的情况是,您的代码错误地计算并尝试执行转换函数

它的reading
reader[“CustomerId”]是DBNull
,实际上它不是一个DBNull.Value,因此它尝试执行
Convert.ToInt32(“”
)并崩溃。您需要修复if语句,并添加一个
(int?
强制转换

应该是:

 Id = reader["CustomerId"] is DBNull.Value ? (int?) null :Convert.ToInt32(reader["CustomerID"]);

发生的情况是,您的代码错误地计算并尝试执行转换函数

它的reading
reader[“CustomerId”]是DBNull
,实际上它不是一个DBNull.Value,因此它尝试执行
Convert.ToInt32(“”
)并崩溃。您需要修复if语句,并添加一个
(int?
强制转换

应该是:

 Id = reader["CustomerId"] is DBNull.Value ? (int?) null :Convert.ToInt32(reader["CustomerID"]);
试试这个

Id = reader["CustomerId"] is DBNull ? (int?)null : Convert.ToInt32(reader["CustomerID"]);
?的两个部分的类型:需要是显式的

Id = reader["CustomerId"] is DBNull ? (int?)null : Convert.ToInt32(reader["CustomerID"]);

两部分的类型?:需要通过“Id”显式显示

您指的是顶部声明的可空CustomerID吗?通过“Id”您指的是顶部声明的可空CustomerID吗?是的,强制转换将解决问题:Id=reader[“CustomerID”]是否为DBNull?(int?)null:Convert.ToInt32(读卡器[“CustomerID”]);我不喜欢投空球。reader.IsDbNull更干净。我说的是条件表达式的“True”部分。不是关于检查读取器是否为DBNull。+1,这将解决特定的错误消息,尽管我认为这不是最好的解决方案。是的,强制转换将解决问题:Id=reader[“CustomerId”]是否为DBNull?(int?)null:Convert.ToInt32(读卡器[“CustomerID”]);我不喜欢投空球。reader.IsDbNull更干净。我说的是条件表达式的“True”部分。不是关于检查读取器是否为DBNull。+1,这将解决特定的错误消息,尽管在我看来这不是最好的解决方案。+1,更好的解决方案。如果您不喜欢多行方法,您可以向数据读取器类添加一个扩展方法,以允许代码中使用单行语法。是否存在使用列名的IsDBNull重载?我只能在MSDN上找到int版本:@justinniessner:不,对不起-那将是我自己的扩展方法。。。。更新我的答案+1,更好的解决方案。如果您不喜欢多行方法,您可以向数据读取器类添加一个扩展方法,以允许代码中使用单行语法。是否存在使用列名的IsDBNull重载?我只能在MSDN上找到int版本:@justinniessner:不,对不起-那将是我自己的扩展方法。。。。更新了我的答案+1以撤销(未注释的)否决票。这是对“此转换有什么问题”的正确回答。+1表示反向(未注释)向下投票。这是对“此转换有什么问题”的正确回答。+1表示反向(未注释)向下投票。这是对“此转换有什么问题”的正确回答。+1表示反向(未注释)向下投票。这是对“这个转换有什么问题”的正确回答。技术上,它会产生一个新的错误,但他最初的错误是因为他试图转换.ToInt32(“”);这将产生一个新的错误,但他最初的错误是因为他试图转换.ToInt32(“”);