Arie
Arie

Reputation: 3553

Variable inside inner interpolation

This one perfectly works:

($"SELECT * FROM {Table} WHERE Hex = 11 {(isConfirmed ? " AND UserId<>5" : string.empty)}";

Nevertheless besides static 5 value i want to put variable there. I tried to change as follows nevertheless something is wrong:

($"SELECT * FROM {Table} WHERE Hex = 11 {(isConfirmed ? " AND UserId<>"{myVariable} : string.empty)}";

What i am doing wrong?

Upvotes: 0

Views: 100

Answers (2)

Pavel Anikhouski
Pavel Anikhouski

Reputation: 23228

You should add an interpolation character $ before inner " AND UserId<>{myVariable}" string to use an interpolation expression inside this string

var isConfirmed = true;
var Table = "test";
var myVariable = 5;
var str = $"SELECT * FROM {Table} WHERE Hex = 11 {(isConfirmed ? $" AND UserId<>{myVariable}" : string.Empty)}";

It'll give you

SELECT * FROM test WHERE Hex = 11 AND UserId<>5

Upvotes: 2

SomeBody
SomeBody

Reputation: 8743

This should work:

string query = $"SELECT * FROM {Table} WHERE Hex = 11 {(isConfirmed ? $" AND UserId<>{myVariable}" : string.Empty)}";

Upvotes: 2

Related Questions