Dapper icon indicating copy to clipboard operation
Dapper copied to clipboard

How to pass array of records in parameters with npgsql

Open FLAMESpl opened this issue 1 year ago • 4 comments

How to pass array of records postgres type as a parameter from c#?

I have tried to send it the same way it is received from query result - as double array of objects, but npgsql complains about it:

            var result = await dbContext.Database.GetDbConnection().QueryAsync<Person>(
                new CommandDefinition(
                    commandText: sql,
                    parameters: new
                    {
                        People = ids
                            .Select(t => new object[]
                            {
                                t.FirstName,
                                t.LastName
                            })
                            .ToArray()
                    },
                    cancellationToken: cancellationToken));

The CLR type System.Object isn't natively supported by Npgsql or your PostgreSQL. To use it with a PostgreSQL composite you need to specify DataTypeName or to map it, please refer to the documentation.

I guess I need to use DynamicParameters object and specify type name manually, but it accepts enum instead of string and in enum there is no such value like object or record.

I need it for query like this one:

SELECT * FROM peopletable
WHERE ROW(firstname, lastname) = ANY(@people)

Can someone guide me how to do it?

FLAMESpl avatar Feb 17 '24 17:02 FLAMESpl

Does string[] work?

mgravell avatar Feb 17 '24 19:02 mgravell

You might need to flatten that - what is the expected parameter values here? If this was raw ADO.NET, what is it that you're trying to invoke?

mgravell avatar Feb 17 '24 19:02 mgravell

@people is of type record[], postgres literal would be

ARRAY[ROW('Nikola', 'Tesla'), ROW('Thomas', 'Edison'), ROW('Georg', 'Ohm')]

however, I think my question is not valid anymore as for some reason it is possible to compare two records in postgres, it is impossible to check if record exists in an array

FLAMESpl avatar Feb 18 '24 16:02 FLAMESpl

I don't think that's a scenario we've had cause to look at. I think for now the answer is "we don't have an option for that" - happy to add it to the pile of candidates if it is a useful / common thing.

mgravell avatar Feb 18 '24 19:02 mgravell