【发布时间】:2018-02-07 12:30:30
【问题描述】:
查询(SQLite)结果将保存在字典中,字典将保存在数组中
string sql = "select * from account";
SqliteConnection m_dbConnection = new SqliteConnection("Data Source = database.sqlite");
m_dbConnection.Open();
SqliteCommand command = new SqliteCommand(sql, m_dbConnection);
SqliteDataReader reader = command.ExecuteReader();
// This is the array
List<Dictionary<string, string>> array = new List<Dictionary<string, string>>();
while (reader.Read())
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
for (int a = 0; a< reader.FieldCount; a++)
{
if (!reader.IsDBNull(a))
{
string key = reader.GetName(a);
string value = reader.GetString(a).ToString();
dictionary.Add(key, value);
//Dictionary will be saved in the array
array.Add(new Dictionary<string, string>(dictionary));
}
}
}
现在我尝试打印出数组中每个字典的键和值,但它只打印出以下内容
for (int i = 0; i < array.Count; i ++)
{
Debug.WriteLine("array(" + i + ") Key=" + array[i].SelectMany(x=> x.Key) + "Value = " + array[i].SelectMany(x => x.Value)+ "\n \n");
}
array(x) Key=System.Linq.Enumerable+SelectManySingleSelectorIterator
2[System.Collections.Generic.KeyValuePair2[System.String,System.String],System.Char]Value = System.Linq.Enumerable+SelectManySingleSelectorIterator2[System.Collections.Generic.KeyValuePair2[System. String,System.String],System.Char]
而不是值和键本身。为什么?
或者你有什么建议我可以不使用列表将字典保存为数组吗?谢谢你。
【问题讨论】:
标签: c# arrays linq dictionary