Skip to content
Advertisement

How to write a multiline SQL string in C# but treat it as a single line string

There is a part in the application where SQL statements are generated. There is a unit test for it that currently only compares if the given query string matches the expected query string. I’m using a multiline string with interpolation (simplified example)

command.Should().Be($@"UPDATE `todo`
                       SET `todo`.`is_done`=0
                       WHERE `todo`.`id`='a9cebb04-fa7d-4071-9f3e-0704bd3352fa'");

Writing it this way the multiline string looks like this

"UPDATE `todo`
                                SET `todo`.`is_done`=0
                                WHERE `todo`.`id`='a9cebb04-fa7d-4071-9f3e-0704bd3352fa'"

The identations are filled with empty characters. The generated SQL string by code is this

"UPDATE `todo` SET `todo`.`is_done`=0 WHERE `todo`.`id`='a9cebb04-fa7d-4071-9f3e-0704bd3352fa'"

The unit test fails because the generated string does not match the expected string. How can I remove the empty characters from the expected string? Because currently the test fails with

Expected string to be … with a length of 377, but … has a length of 206 …

Advertisement

Answer

You can use string concatenation to achieve a similar kind of formatting in source code, and the desired string value:

command.Should().Be("UPDATE `todo` " +
                    "SET `todo`.`is_done`=0 " +
                    "WHERE `todo`.`id`='a9cebb04-fa7d-4071-9f3e-0704bd3352fa'");
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement