如何在 Dapper Query 的动态参数中传递多个条件,就像我们为 IN 运算符传递一样
How to pass multiple condition in dynamic parameter in Dapper Query like we pass for IN operator
我想在查询我的 PostgreSQL 数据库时传递多个实体代码。我收到以下代码的错误:
我希望查询像下面这样工作:
SELECT * FROM public.test WHERE entity_code IN('value1', 'value2');
下面是我的代码,它在我传递单个值时有效:
string query = "SELECT * FROM public.test WHERE entity_code = @Entity_Code";
Dictionary<string, object> dictionary = new Dictionary<string, object>();
dictionary.Add("@Entity_Code", "value1");
var connection = _postgreSqlContext.Database.GetDbConnection();
var results = connection.Query<Test>(query, new DynamicParameters(dictionary));
下面是在针对同一参数添加多个值的情况下不起作用的代码:
string query = "SELECT * FROM public.test WHERE entity_code IN @Entity_Code";
Dictionary<string, object> dictionary = new Dictionary<string, object>();
dictionary.Add("@Entity_Code", new string[]{ "value1", "value2" });
var connection = _postgreSqlContext.Database.GetDbConnection();
var results = connection.Query<Test>(query, new DynamicParameters(dictionary));
Postgres 的 IN
语法略有不同。你应该使用 ANY
:
string query = "SELECT * FROM public.test WHERE entity_code = ANY(@Entity_Code)";
var dictionary = new Dictionary<string, object>();
dictionary.Add("@Entity_Code", new string[]{ "value1", "value2" });
var connection = _postgreSqlContext.Database.GetDbConnection();
var results = connection.Query<Test>(query, new DynamicParameters(dictionary));
查看此问题的答案:IN vs ANY operator in PostgreSQL
我想在查询我的 PostgreSQL 数据库时传递多个实体代码。我收到以下代码的错误: 我希望查询像下面这样工作:
SELECT * FROM public.test WHERE entity_code IN('value1', 'value2');
下面是我的代码,它在我传递单个值时有效:
string query = "SELECT * FROM public.test WHERE entity_code = @Entity_Code";
Dictionary<string, object> dictionary = new Dictionary<string, object>();
dictionary.Add("@Entity_Code", "value1");
var connection = _postgreSqlContext.Database.GetDbConnection();
var results = connection.Query<Test>(query, new DynamicParameters(dictionary));
下面是在针对同一参数添加多个值的情况下不起作用的代码:
string query = "SELECT * FROM public.test WHERE entity_code IN @Entity_Code";
Dictionary<string, object> dictionary = new Dictionary<string, object>();
dictionary.Add("@Entity_Code", new string[]{ "value1", "value2" });
var connection = _postgreSqlContext.Database.GetDbConnection();
var results = connection.Query<Test>(query, new DynamicParameters(dictionary));
Postgres 的 IN
语法略有不同。你应该使用 ANY
:
string query = "SELECT * FROM public.test WHERE entity_code = ANY(@Entity_Code)";
var dictionary = new Dictionary<string, object>();
dictionary.Add("@Entity_Code", new string[]{ "value1", "value2" });
var connection = _postgreSqlContext.Database.GetDbConnection();
var results = connection.Query<Test>(query, new DynamicParameters(dictionary));
查看此问题的答案:IN vs ANY operator in PostgreSQL