【问题标题】:linq check if matches null or selected itemlinq 检查是否匹配 null 或选定项
【发布时间】:2013-05-14 08:36:24
【问题描述】:

我正在检查项目是否已经与我的 MSSQL DB 中的内容匹配。我正在使用 LINQ 来更新记录。我想知道如何检查一个项目是否等于 d_0_2 或者它是否等于 null/empty。我该怎么做呢?

以下是我现有的代码,它部分有效。但由于 null/Empty 而失败

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

提前致谢。

【问题讨论】:

  • 如果updateProduct.studioId 为NULL,你需要你的代码做什么?

标签: c# asp.net sql-server linq


【解决方案1】:

我不确定我是否正确理解了您的问题,但您想检查 item 是否为 null 或者是否 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);
}

【讨论】:

    【解决方案2】:
    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);
    }
    

    【讨论】:

      猜你喜欢
      • 2016-07-04
      • 1970-01-01
      • 2021-08-27
      • 1970-01-01
      • 2020-05-28
      • 2011-11-12
      • 1970-01-01
      • 2020-04-14
      • 1970-01-01
      相关资源
      最近更新 更多