2013-05-14 57 views
0

我正在檢查項目是否已與我的MSSQL數據庫中的項目匹配。我正在使用LINQ來更新記錄。我想知道如何檢查一個項目是否等於d_0_2或者它是否等於null/empty。我將如何去做這件事?linq檢查是否匹配null或選定項目

下面是我現有的代碼,部分工作。但因空/空而失敗

if (updateProduct.studioId == Convert.ToInt32(d_0_2.SelectedValue)) { } 
else { updateProduct.studioId = Convert.ToInt32(d_0_2.SelectedValue);} 

在此先感謝。

+0

你需要什麼你的代碼'updateProduct.studioId'是否爲NULL? – Sonhja 2013-05-14 09:05:33

回答

0
string value = d_0_2.SelectedValue.ToString(); 
// what if SelectedValue is empty or null? 
if (!string.IsNullOrEmpty(value)) 
    return; 
// what if product is null? 
if (updateProduct != null) 
    return; 

if (updateProduct.studioId != null && 
    updateProduct.studioId == Convert.ToInt32(value)) 
{ 
    // you have product and its id equal to d_0_2.SelectedValue 
} 
else 
{ 
    // studioId not equal to to d_0_2.SelectedValue 
    updateProduct.studioId = Convert.ToInt32(value); 
} 
1

我不知道我的理解是否正確的問題,但你要檢查的項目爲空或如果不被它studioId等於d_0_2.SelectedValue

if (updateProduct == null) 
{ 
    //create new update product 
} 
else if (updateProduct.studioId != Convert.ToInt32(d_0_2.SelectedValue)) 
{ 
    updateProduct.studioId = Convert.ToInt32(d_0_2.SelectedValue); 
} 
相關問題