'C# 7 string interpolation after reading string from database

I have a string coming from database.

var query = @"Select SKUs from dbo.Order order by OrderDate OFFSET {skip} ROWS FETCH NEXT {take} ROWS ONLY"

This string is coming from database directly, so I dont have this string formed in code. I want to use $ interpolation to replace them

var skip = 0;
var take = 200;
query = $query

Is there any way I can do this? I know I can use replace or string.format but just curious if we can do this.



Solution 1:[1]

The $"" syntax is only for string literals.

The $ special character identifies a string literal as an interpolated string. An interpolated string is a string literal that might contain interpolation expressions.

$ - string interpolation (C# reference)

String literals only exist in the source code. Anything you read from a database is a string object and cannot be a literal.

As you mentioned, you need to use conventional methods to manipulate the string's data (string.Format, regex, etc).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gunr2171