2014-02-14 92 views
0
Convert.ToString(icdoCalcWiz.ihstOldValues[enmCalcWiz.position_value.ToString()])!=icdoCalcWiz.position_value 

這裏LHS值會爲「」(空)轉換空爲Null

但在RHS值將是NULL

我必須滿足的條件一樣,如果爲空或null兩者相等

但按照上述條件都不爲:

回答

0

什麼

string lhs = Convert.ToString(icdoCalcWiz.ihstOldValues[enmCalcWiz.position_value.ToString()]); 
string rhs = icdoCalcWiz.position_value; 

if ((lhs == null || lhs == string.Empty) && (rhs == null || rhs == string.Empty)){ 
    return true; 
} else { 
    return lhs == rhs; 
} 
4

使用本。

if (string.IsNullOrEmpty("any string")) 
{ 
} 

還有一種方法String.IsNullOrWhitespace(),其指示一個指定的字符串是否爲空,空的,或僅包含空白字符。

if(String.IsNullOrWhitespace(val)) 
{ 
    return true; 
} 

以上是下面的代碼的快捷方式:

if(String.IsNullOrEmpty(val) || val.Trim().Length == 0) 
{ 
    return true; 
} 
1

你可以試試這個....

static string NullToString(object Value) 
{ 

    // Value.ToString() allows for Value being DBNull, but will also convert int, double, etc. 
    return Value == null ? "" : Value.ToString(); 

    // If this is not what you want then this form may suit you better, handles 'Null' and DBNull otherwise tries a straight cast 
    // which will throw if Value isn't actually a string object. 
    //return Value == null || Value == DBNull.Value ? "" : (string)Value; 
} 

你的代碼寫成....

Convert.ToString(icdoCalcWiz.ihstOldValues[enmCalcWiz.position_value.ToString()])!= 
NullToString(icdoCalcWiz.position_value) 
相關問題