2013-05-14 58 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); 
} 
相关问题