如何检查 DataRow 中的值是否为零
How to check values is zero inside DataRow
在我的代码中,我通过迭代 DataRow
向 foreach
中的值添加了一个列表。我的代码如下,
ActivityUserResult Result = new ActivityUserResult();
Result.TransacDetails = new List<TransactionStatistics>();
foreach (DataRow row in ds.Tables[2].Rows)
{
Result.TransacDetails.Add(new TransactionStatistics()
{
Id = Convert.ToString(row["Id"]),,
AssistCount = Convert.ToInt32(row["AssistCount"]),
WaitTime = Convert.ToInt32(row["WaitTime"]),
AverageStandardWaitTime = (Convert.ToInt32(row["WaitTime"]) / Convert.ToInt32(row["AssistCount"])) / 60
});
}
我需要用 WaitTime
除以 AssistCount
得到 AverageStandardWaitTime
值。为此,我在循环
中添加了这个
AverageStandardWaitTime = (Convert.ToInt32(row["WaitTime"]) / Convert.ToInt32(row["AssistCount"])) / 60
但有时 AssistCount
值将为零。所以我收到了这个错误。
System.DivideByZeroException: 'Attempted to divide by zero.'
如何检查 AssistCount
值是否为零?如果不为零,我需要除法,否则我需要避免除法,需要将 AverageStandardWaitTime
值设置为 0
您可以为此使用三元运算符,这非常简单:
AverageStandardWaitTime = (Convert.ToInt32(row["AssistCount"]) > 0
? Convert.ToInt32(row["WaitTime"]) / Convert.ToInt32(row["AssistCount"])
: 0)
如果你想重复使用转换后的值,而不是每次都转换值,你可以做得更好。
foreach (DataRow row in ds.Tables[2].Rows)
{
Result.TransacDetails.Add(new TransactionStatistics()
{
Id = Convert.ToString(row["Id"]),,
AssistCount = Convert.ToInt32(row["AssistCount"]),
WaitTime = Convert.ToInt32(row["WaitTime"]),
AverageStandardWaitTime = (Convert.ToInt32(row["WaitTime"]) > 0
? (Convert.ToInt32(row["WaitTime"]/Convert.ToInt32(row["AssistCount"]))/60
: Convert.ToInt32(row["WaitTime"])
)
});
}
您可以创建函数然后调用它,这样您就可以获得清晰的代码和关于数据出错的正确消息
public double CalculateAverageStandardWaitTime(DataRow row)
{
int waitTime = Convert.ToInt32(row["WaitTime"];
int assistcount = Convert.ToInt32(row["AssistCount"]);
if(assistcount != 0 ) {
return (Convert.ToInt32(row["WaitTime"]) /
Convert.ToInt32(row["AssistCount"])) / 60;
}
else {
//log message that value is less then zeor
//return some defautl value
return -1; //0
}
}
您的代码将是
Result.TransacDetails.Add(new TransactionStatistics()
{
Id = Convert.ToString(row["Id"]),,
AssistCount = Convert.ToInt32(row["AssistCount"]),
WaitTime = Convert.ToInt32(row["WaitTime"]),
AverageStandardWaitTime = CalculateAverageStandardWaitTime(row)
});
在我的代码中,我通过迭代 DataRow
向 foreach
中的值添加了一个列表。我的代码如下,
ActivityUserResult Result = new ActivityUserResult();
Result.TransacDetails = new List<TransactionStatistics>();
foreach (DataRow row in ds.Tables[2].Rows)
{
Result.TransacDetails.Add(new TransactionStatistics()
{
Id = Convert.ToString(row["Id"]),,
AssistCount = Convert.ToInt32(row["AssistCount"]),
WaitTime = Convert.ToInt32(row["WaitTime"]),
AverageStandardWaitTime = (Convert.ToInt32(row["WaitTime"]) / Convert.ToInt32(row["AssistCount"])) / 60
});
}
我需要用 WaitTime
除以 AssistCount
得到 AverageStandardWaitTime
值。为此,我在循环
AverageStandardWaitTime = (Convert.ToInt32(row["WaitTime"]) / Convert.ToInt32(row["AssistCount"])) / 60
但有时 AssistCount
值将为零。所以我收到了这个错误。
System.DivideByZeroException: 'Attempted to divide by zero.'
如何检查 AssistCount
值是否为零?如果不为零,我需要除法,否则我需要避免除法,需要将 AverageStandardWaitTime
值设置为 0
您可以为此使用三元运算符,这非常简单:
AverageStandardWaitTime = (Convert.ToInt32(row["AssistCount"]) > 0
? Convert.ToInt32(row["WaitTime"]) / Convert.ToInt32(row["AssistCount"])
: 0)
如果你想重复使用转换后的值,而不是每次都转换值,你可以做得更好。
foreach (DataRow row in ds.Tables[2].Rows)
{
Result.TransacDetails.Add(new TransactionStatistics()
{
Id = Convert.ToString(row["Id"]),,
AssistCount = Convert.ToInt32(row["AssistCount"]),
WaitTime = Convert.ToInt32(row["WaitTime"]),
AverageStandardWaitTime = (Convert.ToInt32(row["WaitTime"]) > 0
? (Convert.ToInt32(row["WaitTime"]/Convert.ToInt32(row["AssistCount"]))/60
: Convert.ToInt32(row["WaitTime"])
)
});
}
您可以创建函数然后调用它,这样您就可以获得清晰的代码和关于数据出错的正确消息
public double CalculateAverageStandardWaitTime(DataRow row)
{
int waitTime = Convert.ToInt32(row["WaitTime"];
int assistcount = Convert.ToInt32(row["AssistCount"]);
if(assistcount != 0 ) {
return (Convert.ToInt32(row["WaitTime"]) /
Convert.ToInt32(row["AssistCount"])) / 60;
}
else {
//log message that value is less then zeor
//return some defautl value
return -1; //0
}
}
您的代码将是
Result.TransacDetails.Add(new TransactionStatistics()
{
Id = Convert.ToString(row["Id"]),,
AssistCount = Convert.ToInt32(row["AssistCount"]),
WaitTime = Convert.ToInt32(row["WaitTime"]),
AverageStandardWaitTime = CalculateAverageStandardWaitTime(row)
});