【发布时间】:2011-10-19 17:29:04
【问题描述】:
我正在使用 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;
}
}
谢谢
【问题讨论】:
标签: c# c#-4.0 dapper expando expandoobject