简体   繁体   中英

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. 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. 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. To check if an element exists in a list, use the following syntax: WHERE "CreatedById" = ANY (:idList) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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