我正在使用Dapper将一个2列结果集提取到一个字典中。
我注意到当我将鼠标悬停在结果集上时,智能提示会显示.ToDictionary()方法,但是我无法使其工作,因为dapper使用动态属性/ExpandoObject。
Dictionary<string, string > rowsFromTableDict = new Dictionary<string, string>();
using (var connection = new SqlConnection(ConnectionString))
{
connection.Open();
var results = connection.Query
("SELECT col1 AS StudentID, col2 AS Studentname
FROM Student order by StudentID");
if (results != null)
{
//how to eliminate below foreach using results.ToDictionary()
//Note that this is results<dynamic, dynamic>
foreach (var row in results)
{
rowsFromTableDict.Add(row.StudentID, row.StudentName);
}
return rowsFromTableDict;
}
}
谢谢你