短手如果声明没有别的

mar*_*sey 6 c# linq lambda

我正在尝试使用if语句的简写,因为我正在构建表达式查询,如果test为null,则访问器会导致错误.

test != null ? test.Contains("mystring") : NO_VLAUE
Run Code Online (Sandbox Code Playgroud)

我在寻找:

test != null ? test.Contains("mystring")

otherwise ignore.
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用??for,is null但是有反转.

提前致谢.

SLa*_*aks 8

这听起来像你想要的 test != null && test.Contains("mystring")

你问的问题没有意义.所有表达式(包括条件运算符)都必须具有值.您期望该表达式评估为if test是什么?

如果test为null,您可能希望它为false.
换句话说,如果test不为null且包含,则希望它为true mystring.


Jon*_*eet 7

听起来你可能想要:

test != null && test.Contains("mystring")
Run Code Online (Sandbox Code Playgroud)

这将评估falseif是否test为null - 这是你想要的吗?基本上你需要说出你想要发生什么,如果test null,否则它不能用作表达式.