dapper - return 一个 INT 列表

dapper - return a list of INT

我从一个存储过程中返回一个 int 的列表 - 就像

SELECT ID FROM Table

这是在使用 dapper。以下是我正在尝试做的尝试。我可以使用相同的方法填充对象,但不能填充 int 对象

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();

我认为它与语句末尾的 => new int 有关。我相信解决方案相当简单。只是其中一个星期五。

我认为您应该像下面这样从查询中指定您期望的类型:

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

您可以查看此 Execute a query and map the results to a strongly typed List 了解更多信息。

这非常相似,但 .ToList() as IList<int>

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