Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

if (record["Dosage"].ToString() != string.Empty)
    result.StartsWith("https://stackoverflow.com/") && result.EndsWith("https://stackoverflow.com/") ? result.Replace("https://stackoverflow.com/", string.Empty) : result;

The second line there is not a statement, it is an expression. Not all expressions can be statements in C#, so this is a syntax error.

Presumably you meant to assign the result of this to result:

if (record["Dosage"].ToString() != string.Empty)
    result = (result.StartsWith("https://stackoverflow.com/") && result.EndsWith("https://stackoverflow.com/")) ? result.Replace("https://stackoverflow.com/", string.Empty) : result;

Further, you should consider enclosing the bodies of if / else blocks with braces ({}). Without braces, the way in which these blocks nest is not intuitive, and it will hamper future maintenance. (For example, can you tell which if block the else if block will belong to? Will the next person who inherits this project be able to not only tell the difference, but understand what nesting was intended? Make it explicit!)

Leave a Comment