Created
March 28, 2022 08:33
-
-
Save alastair-todd/fcdb880265581b133809bd45ae14cd22 to your computer and use it in GitHub Desktop.
Form a text search query (tsquery) with dbcontext / ado.net
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<NpgsqlTsQuery> ToTsQuery(DbContext dbContext, string searchTerm) | |
{ | |
var connection = dbContext.Database.GetDbConnection() as NpgsqlConnection; | |
await using var cmd = new NpgsqlCommand("select plainto_tsquery('english', @search_term)", connection); | |
cmd.CommandType = CommandType.Text; | |
if (connection!.State != ConnectionState.Open) connection.Open(); | |
cmd.Parameters.Add(new NpgsqlParameter("search_term", searchTerm)); | |
var value = await cmd.ExecuteScalarAsync(); | |
var tsVector = value == DBNull.Value || value == null ? null : value as NpgsqlTsQuery; | |
return tsVector; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment