0

Notice this question is related to: Compose GraphQL for monday.com

But this question only focus on the concatenation part.

I have following string:

var query = @"{""query"": ""mutation {create_item(board_id: 111, group_id:\""new_group\"", item_name: \""adding works\"", column_values: \"" {\\\""long_text\\\"": { \\\""text\\\"": \\\""Sample text\\\""}} \"") {id}  }"" }";

I want to build this string dynamically. But I don't know to.

I tried:

var query = @"{""query"": ""mutation {create_item(board_id: 111, group_id:\""new_group\"", item_name: \""adding works\"", column_values: \"" {\\\""long_text\\\"": { \\\""text\\\"": \\\"""+"SOME TEXT HERE"+"\\\""}} \"") {id}  }"" }";

Is there some guideline to concatenate strings with so many escapes?

Ganesh Jadhav
  • 712
  • 6
  • 22
Thomas Segato
  • 4,567
  • 11
  • 55
  • 104
  • 2
    You need to include the `@` before each of the strings you concatenate together, or at least the ones that have backslashes that should not be escape sequences and double quotes escaped by another double quote. – juharr Aug 31 '20 at 14:51
  • 1
    The guideline is: "carefully". – Heretic Monkey Aug 31 '20 at 14:54
  • Does this answer your question? [Concatenating two strings with escape sequences](https://stackoverflow.com/questions/19832281/concatenating-two-strings-with-escape-sequences) this explains what you need. – Trevor Aug 31 '20 at 14:59
  • Not really. Normally it is no deal concatenate strings. I also tried with @. As an example I tried: { \\\""text\\\"": \\\"""+@""+"\\\""}} but compiler fails. – Thomas Segato Aug 31 '20 at 15:56
  • I am not sure from your example but it looks like you are storing data in a JSON format. The normal way to handle that is to define a class to represent the data then use a tool to serialize it into a JSON string (see [link](https://stackoverflow.com/questions/6201529/how-do-i-turn-a-c-sharp-object-into-a-json-string-in-net) or [link](https://stackoverflow.com/questions/11345382/convert-object-to-json-string-in-c-sharp)) – John Wu Aug 31 '20 at 16:38

1 Answers1

0

There is no real guideline except that you need to prefix all strings with the verbatim string literal character (@) if they have escape characters that you want to include as part of the string (you are missing one in front of the last string in the example from the question):

var query1 = @"{""query"": ""mutation {create_item(board_id: 111, group_id:\""new_group\"", item_name: \""adding works\"", column_values: \"" {\\\""long_text\\\"": { \\\""text\\\"": \\\""" 
    + "SOME TEXT HERE" 
    + @"\\\""}} \"") {id}  }"" }";
Rufus L
  • 36,127
  • 5
  • 30
  • 43