Dapper - Returns an INT List

From a stored procedure, I return a list int- just like

SELECT ID FROM Table

It uses a dapper. The following is an attempt at what I'm trying to do. I can fill the object using the same approach but not the objectint

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 that this is somehow related to => new intthe end of the statement. I am sure the solution is quite simple. just one of them on Fridays.

+7
source share
2 answers

I think you should specify the type that you expect from your request, as shown below:

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

You can verify this. Run the query and match the results with a strongly typed list for more information.

+12
source

It is very similar, but with .ToList() as IList<int>

var ids = connection.Query<int>("storedprocedure", 
param, transaction:transaction, commandType: CommandType.StoredProcedure)
.ToList() as IList<int>
+2
source

Source: https://habr.com/ru/post/1648746/


All Articles