I have a dynamic query that I need to build based on a value passed in the URL parameters. The value passed can be null or with value. In this example, the parameter is the title. I'm using the string parameter placeholder with the FromSql C# fonction :
var query = this.BookContext.BookModel.FromSql(
@"SELECT FROM Books b
WHERE b.title = {0}, title)
If the title has a value, the query works fine, but I have a problem when the title is null.
When the title is null, the condition should be b.title IS NULL since b.title = NULL will not work.
My question is how to pass IS NULL
condition in a string parameter placeholder?
I tried to build a dynamic condition but it's not allowed in a string parameter placeholder and it makes sense since the string parameter placeholder will have no benefit
string bookCondition = title != "null" ? title : "title IS NULL";
var query = this.BookContext.BookModel.FromSql(
@"SELECT FROM Books b
WHERE b.title {0}, bookCondition)
Thanks,