Tôi đang sử dụng Dapper để lấy một kết quả 2 cột vào từ điển. tôi nhận thấy rằng IntelliSense cho tôi một .ToDictionary() khi tôi di chuột qua resultset nhưng tôi không thể có được nó để làm việc kể từ đoan trang sử dụng tính năng động/expandoObjectlàm thế nào để chuyển đổi từ điển <động, động> sang từ điển <string, string> bằng cách sử dụng Colllection.ToDictionary()
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ảm ơn bạn
yes. điều đó. cảm ơn – Gullu
+1 để nhớ bỏ qua 'động'. –
Động là tốt đẹp, nhưng nó có rất nhiều bẫy như cần phôi trở lại các loại tĩnh. –