2016-10-28 72 views
0

我有這個代碼檢查Excel文件中的單元格。如果條件在C#

var a = worksheet.Cells[i, 4].StringValue; 
if (!string.IsNullOrEmpty(a)) 
{ 
    table.A = (DateTime?)GetResult(UploaddataBase.ProcessDateTime(a, i, null), "A").Value; 

    if (table.A.Value < table.B.Value) 
    { 
     unexpectedExceptions.Add(i.ToString(), string.Format("A should be less than B for row number : {0}", (i + 1))); 
    } 
} 

我要檢查,如果table.B.Value,無論它存在與否的文件,以檢查此條件if (table.A.Value < table.B.Value)

這可以這樣做?

if(!string.IsNullOrEmpty(a) && !string.IsNullOrEmpty(b)) 
else if(string.IsNullOrEmpty(a) && string.IsNullOrEmpty(b)) 

還是這是另一個whay?

+0

例外不隱意外;)? – Aphelion

+0

我認爲這段代碼甚至不會編譯。 'var = a ='應該是'var a =' –

+0

@MohitShrivastava這是一個錯誤。 –

回答

0

假定該table.Atable.BDateTime?類型的,你可以做...

if(table.B.HasValue && table.A.HasValue && (table.A.Value < table.B.Value)) 
{ 
    unexpectedExceptions.Add(
     i.ToString(), 
     string.Format("A should be less than B for row number : {0}", (i + 1))); 
} 
+0

這是正確的。謝謝! –

+1

你在if語句末尾忘了一個paranthesis – meJustAndrew