尝试catch为单行if语句

Rat*_*tan -1 c# if-statement try-catch

我需要编写一个使用(单行if)语句的语法,但我需要在某种意义上嵌套它:

(表达式1)?

(如果表达式2抛出ArgumentExceptionstring.empty其他expression2):string.empty

所以基本上我需要弄清楚在c#中单行if if语句中使用try catch的语法(单行因为我需要在linq to sql select语句中使用它.)

单行if语句我的意思是使用三元运算符if语句而没有任何分号.

Sam*_*ica 6

你在找这样的东西吗?

(/*expression1*/)? (foo()? string.empty: /*expression2*/):string.empty
Run Code Online (Sandbox Code Playgroud)

与foo:

public /*static?*/ bool foo()
{
    try
    {
        //do stuff
    }
    catch(ArgumentException)
    {
        return true
    }
    catch
    {
        return false
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

至于将Try/Catch嵌入到Linq to Sql语句中...好吧,我只想说在这种情况下我会尝试重新考虑我的设计.