代码之家  ›  专栏  ›  技术社区  ›  Eton B.

比较C中可能的空字符串#

  •  2
  • Eton B.  · 技术社区  · 14 年前

    我尝试使用以下方法比较数据库中LINQ查询的字符串字段:

    e.Comment.equals("Working From Home")
    

    关于WHERE子句。

    Object reference not set to an instance of an object 例外。

    有没有什么方法可以检查注释是否为空,然后进行比较以避免异常?

    4 回复  |  直到 14 年前
        1
  •  4
  •   Mark Byers    14 年前

    你可以用 == 而不是等于:

    e.Comment == "Working From Home"
    

        2
  •  1
  •   jeroenh    14 年前

    在这种情况下,使用“==”运算符就足够了:

    e.Comment == "Working From Home"
    
        3
  •  0
  •   Andrew    14 年前
    !String.IsNullOrEmpty(e.Comment) && e.Comment == "Working From Home"
    
        4
  •  0
  •   josef.axa    14 年前

    你能做到的 "Working From Home".Equals(e.Comment); String.Equals(e.Comment, "Working From Home");