I have a model that has a HashSet<int>
member. Since I wasn't able to get Dapper to automatically create the set from an array, I thought I would just use a multi-mapping query and get the array as a separate column, split on it and create the set inside a lambda. Just to be clear, I want to deserialize the whole model, not just the bit for the set.
My first multi-mapping attempt also failed, so I decided to try something simpler, get an array and deserialize it into a List<int>
or int[]
. And this is where I'm stuck.
I am using a PostgreSQL 12 database.
Code snippet is as follows
var query = "SELECT ARRAY[1, 2, 3];";
var ints1 = await _conn.QueryAsync<List<int>>(query); // This returns an empty array
var ints2 = await _conn.QueryAsync<int[]>(query); // System.ArgumentException: Invalid type owner for DynamicMethod.
I'm not really sure what I'm doing wrong and Google doesn't really seem to help, I only find questions about using lists in queries.
Edit: Ok, I got it working by converting the array into a JSON, but I still find this solution ugly. Is there a way to avoid creating JSONs?
SELECT ARRAY[1, 2, 3]
does not seem to be common SQL, postgres or no? I'd probably suggest that Dapper possibly just doesn't support this pretty funky syntaxquery
return 3 rows of one column? One row of 3 columns? Or one row, once column?{1, 2, 3}
SELECT pg_typeof(ARRAY[1,2,3])
returns "integer[]", so I was expecting Dapper to somehow know that is not a string. Also, getting the result as a string throws an exception.