Search code examples
c#dapper

dapper - return a list of INT


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.


Solution

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