简体   繁体   English

如何在Npgsql和Dapper中正确使用列表作为参数

[英]How do I properly use a List as a parameter with Npgsql and Dapper

I can't figure out how to use a List<Guid> as a parameter in my query. 我不知道如何在查询中使用List<Guid>作为参数。 I've had trouble with this for some time in different situations, but here is the current one. 在不同的情况下我已经遇到了一段时间的麻烦,但这是当前的情况。 I'm trying to cleanup after tests by deleting everything created by a test user. 我正在尝试通过删除测试用户创建的所有内容来清理测试后的内容。 Here's the code: 这是代码:

var idList = new List<Guid>() { SYS_ADMIN_GUID, OPERATOR_GUID, OPERATOR_ELECTRONICS_TECH_GUID, UNIT_MANAGER_GUID, AREA_MANAGER_GUID };

using (NpgsqlConnection c = new NpgsqlConnection(TestHelper.ConnectionString))
{
    c.Open();

    c.Execute(@"delete from ""BlueStakes"".""MarkRequestStatuses"" where ""CreatedById"" in :idList", new { idList });
}

I've also tried to use @idList as the parameter which didn't work either. 我也尝试过使用@idList作为也@idList的参数。 This is the error that it is giving: 这是它给的错误:

Npgsql.PostgresException: Npgsql.PostgresException: 42601: syntax error at or near "$1"

Obviously the query isn't recognizing the list and sticking it in for the parameter but I can't figure out why. 显然,查询无法识别列表并将其插入参数中,但我不知道为什么。

You can't use IN with a list in PostgreSQL. 您不能将IN与PostgreSQL中的列表一起使用。 To check if an element exists in a list, use the following syntax: WHERE "CreatedById" = ANY (:idList) . 要检查列表中是否存在元素,请使用以下语法: WHERE "CreatedById" = ANY (:idList)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM