Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to pass array of records in parameters with npgsql #2046

Open
FLAMESpl opened this issue Feb 17, 2024 · 4 comments
Open

How to pass array of records in parameters with npgsql #2046

FLAMESpl opened this issue Feb 17, 2024 · 4 comments

Comments

@FLAMESpl
Copy link

FLAMESpl commented Feb 17, 2024

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 FLAMESpl changed the title How to pass record in parameters with npgsql How to pass array of records in parameters with npgsql Feb 17, 2024
@mgravell
Copy link
Member

Does string[] work?

@mgravell
Copy link
Member

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?

@FLAMESpl
Copy link
Author

@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

@mgravell
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants