Last active
March 28, 2022 08:34
-
-
Save alastair-todd/c2681c4b642c3c34a348c20358bba157 to your computer and use it in GitHub Desktop.
Form a text search vector (tsvector) 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<NpgsqlTsVector> ToTsVector(DbContext dbContext, string content) | |
{ | |
var connection = dbContext.Database.GetDbConnection() as NpgsqlConnection; | |
await using var cmd = new NpgsqlCommand("select to_tsvector('english', @content)", connection ); | |
cmd.CommandType = CommandType.Text; | |
if (connection!.State != ConnectionState.Open) connection.Open(); | |
cmd.Parameters.Add(new NpgsqlParameter("content", content)); | |
var value = await cmd.ExecuteScalarAsync(); | |
var tsVector = value == DBNull.Value || value == null ? null : value as NpgsqlTsVector; | |
return tsVector; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment