Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dapper - return a list of INT

Tags:

c#

dapper

From a stored procedure I am returning a list of int - simply as

SELECT ID FROM Table

This is using dapper. Below is an attempt of what I am trying to do. I can fill an object in using the same approach but not an int object

List<int> ids = new List<int>();
int id = 0;

// Trying to fill a list of ints here
ids = connection.Query("storedprocedure", param, transaction: transaction, commandType: CommandType.StoredProcedure).Select(row => new int { id = row.ID }).ToList();

I believe its something to do with => new int at the end of the statement. I am sure the solution is fairly simple. Just one of them fridays.

like image 694
user3428422 Avatar asked Jul 22 '16 08:07

user3428422


1 Answers

I think that you should specify the type you expect from your query like below:

var ids = connection.Query<int>("storedprocedure", 
                                param, 
                                transaction: transaction, 
                                commandType: CommandType.StoredProcedure);

You could check this Execute a query and map the results to a strongly typed List for further info.

like image 182
Christos Avatar answered Sep 18 '22 13:09

Christos