使用 Dapper 和 Npgsql 无法从 Guid 列表中 select
Using Dapper and Npgsql unable to select from a list of Guids
以之前post为例;我无法使用 IN 和 Guids 列表进行查询;根据我的尝试,我会收到不同的错误...
public class DataAccess
{
string _connectionString = "{your connection string}";
public async Task<IEnumerable<CustomerDto>> GetListAsync(List<Guid> customers)
{
const string query = @"
SELECT Id,
Name
FROM Customers
WHERE Id IN @CustomerIdList
";
using (var c = new SqlConnection(_connectionString))
{
return await c.QueryAsync<CustomerDto>(query, new { CustomerIdList = customers.ToArray() });
}
}
}
以上失败 42601: syntax error at or near ""
我尝试了如下各种方法,但都失败了 42601: syntax error at or near ""
:
return await c.QueryAsync<CustomerDto>(query, new { CustomerIdList = new[] { customers[ 0 ], customers[ 1 ], customers[ 2 ], customers[ 3 ] } } );
谁能帮忙,我做错了什么?
编辑:由于从另一个问题复制示例代码示例而导致的固定查询
Found out 你必须使用不同的子句,因为 IN 不适用于 Postgresql 中的参数数组,这对我来说听起来有点缺陷:
WHERE Id = ANY(@CustomerIdList)
以之前post为例;我无法使用 IN 和 Guids 列表进行查询;根据我的尝试,我会收到不同的错误...
public class DataAccess
{
string _connectionString = "{your connection string}";
public async Task<IEnumerable<CustomerDto>> GetListAsync(List<Guid> customers)
{
const string query = @"
SELECT Id,
Name
FROM Customers
WHERE Id IN @CustomerIdList
";
using (var c = new SqlConnection(_connectionString))
{
return await c.QueryAsync<CustomerDto>(query, new { CustomerIdList = customers.ToArray() });
}
}
}
以上失败 42601: syntax error at or near ""
我尝试了如下各种方法,但都失败了 42601: syntax error at or near ""
:
return await c.QueryAsync<CustomerDto>(query, new { CustomerIdList = new[] { customers[ 0 ], customers[ 1 ], customers[ 2 ], customers[ 3 ] } } );
谁能帮忙,我做错了什么?
编辑:由于从另一个问题复制示例代码示例而导致的固定查询
Found out 你必须使用不同的子句,因为 IN 不适用于 Postgresql 中的参数数组,这对我来说听起来有点缺陷:
WHERE Id = ANY(@CustomerIdList)