C# Postgresql使用Dapper强制;任何(值(),()…)”;要一份清单

C# Postgresql使用Dapper强制;任何(值(),()…)”;要一份清单,c#,postgresql,dapper,C#,Postgresql,Dapper,我有这样一个问题: SELECT restaurant_id as Id FROM restaurants WHERE unique_key = ANY(ARRAY['key1', 'key2', ...]) 根据本文,使用值而不是数组可以提高性能。我也尝试过该解决方案,查询的性能更好: SELECT restaurant_id as Id FROM restaurants WHERE unique_key = ANY(VALUES('key1'), ('key2'), ...) 但我正在通

我有这样一个问题:

SELECT restaurant_id as Id FROM restaurants WHERE unique_key = ANY(ARRAY['key1', 'key2', ...])
根据本文,使用
而不是
数组
可以提高性能。我也尝试过该解决方案,查询的性能更好:

SELECT restaurant_id as Id FROM restaurants WHERE unique_key = ANY(VALUES('key1'), ('key2'), ...)
但我正在通过Dapper传递
密钥列表

SELECT restaurant_id as Id FROM restaurants WHERE unique_key = ANY(@keyList)
并将查询转换为数组:

SELECT restaurant_id as Id FROM restaurants WHERE unique_key = ANY($1)
我的问题是如何强制Dapper使用带有
值的查询

这是一个大致的实现:

var query = @"SELECT restaurant_id as Id FROM restaurants WHERE unique_key = ANY(@keyList)";

var keyList = new[] { "key1", "key2", ... };
List<int> list;
using (var connection = new NpgsqlConnection(connectionString))
{
    list = await connection.QueryAsync<int>(query, new {keyList});
}
var query=@“从unique_key=ANY(@keyList)的餐厅中选择餐厅id作为id”;
var keyList=new[]{“key1”、“key2”、…};
名单;
使用(var连接=新的NpgsqlConnection(connectionString))
{
list=wait connection.QueryAsync(查询,新{keyList});
}

这在不同的数据库中不受相同的支持,Dapper不知道您使用的是哪种DBMS,因此我认为您无法让Dapper这样做。我会自己建造它,就像:

var-keyList=new[]{“key1”、“key2”、…};
var keyValues=String.Join(“,”,keyList.Select(x=>$”(“{x}”)”);
var query=$@“从unique_key=ANY(value{keyValues})的餐厅中选择餐厅_id作为id”;
名单;
使用(var连接=新的NpgsqlConnection(connectionString))
{
list=wait connection.querySync(查询);
}

如果
keyList
是用户输入,您当然应该采取预防措施,防止SQL注入。

您如何通过dapper?你能添加代码的相关部分吗?我添加了@DervişKayımbaşıoğlu的实现