if (statement)
base.Database.AddInParameter(command, "ColumnX", DbType.String, "StackOverflow");
else
base.Database.AddInParameter(command, "ColumnX", DbType.String, DBNull.Value);
I want to do this statement in one line by using
? "true" : "false"
For example:
if(statement) ? base.Database.AddInParameter(command, "ColumnX", DbType.String, "StackOverflow") : base.Database.AddInParameter(command, "ColumnX", DbType.String, DBNull.Value);
but this is not true. Visual studio says that unexpected token for ” : “
any idea? thanks
First of all, the
ifbefore(statement)is not needed for the conditional operator.Secondly, you can’t use it as both sides of the conditional need to be the same type or types where one is implicitly convertible to the other –
stringandDBNull.Valueare not such types.You can use it if you cast either the
stringorDBNull.Valuetoobject:However, if I had to read this on one line, I would find it less readable than the original version you posted – there is no value in having this logic in one line, in particular if it reduces readability and makes the code less easy to understand.