將Dictionary
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;
}
}
謝謝
是的。那是做的。感謝 – Gullu
+1,因爲它記住了'動態'。 –
動態很好,但確實有很多陷阱需要轉換回靜態類型。 –