public List<IDictionary<string, object>> DapperSelect(string connectionString, string query, object parameters)
{
using (var connection = new SqlConnection(connectionString))
{
var result = connection.Query(query, parameters).ToList();
return result.Select(x => (IDictionary<string, object>)x).ToList();
}
}
IDictionary<string, string>
,因为这并不会产生你想要的预期效果,字典中的每个项可能是布尔值、整型、双精度等类型......result.Select(x => ((IDictionary<string, object>)x).ToDictionary(ks => ks.Key, vs => vs.ToString())).ToList();
但我不建议这么做。connection.Query<YOURTYPE>(query, parameters).ToList();
var foo = db.Query(
"MySp",
new { parameters },
commandType: CommandType.StoredProcedure)
.ToDictionary(
row => (int) row.Id,
row => (string) row.Name);
在这里,row是列名,foo是字典类型。