Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

dapper

npgsql

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.

like image 955
GBreen12 Avatar asked Oct 31 '25 15:10

GBreen12


1 Answers

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).

like image 82
Shay Rojansky Avatar answered Nov 02 '25 03:11

Shay Rojansky