Is it possible to tell linq2db to generate string literals instead of variables?
See original GitHub issueHello! I’ve got the following issue. I am trying to use Sql Server 2016 JSON_VALUE functionality via linq2db’s Sql.Expression attribute. I’ve created the following expression:
[Sql.Expression("JSON_VALUE(FileAttributesJsonColumn, {0})", ServerSideOnly = true)]
public static object JsonValue(string propertyName) => throw new InvalidOperationException();
Then I use it like that:
var files = fileTable.Where(_ => SqlExtensions.JsonValue("$.Extension") == "pdf").ToArray();`
In this case linq2db generates pretty valid where clause:
JSON_VALUE(FileAttributesJson, N'$.Extension') = N'pdf'
However when i try to use variable instead of string literal as property name I receive an error:
var value = "$.Extension";
var files = fileTable.Where(_ => SqlExtensions.JsonValue(value) == "pdf").ToArray();
System.Data.SqlClient.SqlException : The argument 2 of the "JSON_VALUE or JSON_QUERY" must be a string literal.
That happens because in this case linq2db generates variable:
DECLARE @value1 NVarChar(4000) -- String
SET @value1 = N'$.Extension'
and uses it in where clause:
JSON_VALUE(FileAttributesJson, @value1) = N'pdf'
while Sql Server 2016 allows only literals as JSON_VALUE arguments.
Is there a way to tell linq2db to inject C# variable value directly into SQL string literal instead of variable? Thanks a lot!
Issue Analytics
- State:
- Created 5 years ago
- Comments:23 (21 by maintainers)
I had to. Otherwise i will spend a lot of much more time explaining. Sample is usually better 😃 Also i have never created such converters before, so it was my personal experience and i saw new ways how to improve this area.
@CoskunSunali there is closest solution that i can advise. Also you can handle Expression tree before converting SQL, but i have no time to play with that.