如果我按如下方式使用词典(注意:词典在初始化后不会更改)
public static void Main()
{
Dictionary<string, string> mapColumns = new Dictionary<string, string>()
{
{"IDCode", "EmployeeUID"},
{"firstName", "Name"},
{"lastname", "Surname"}
};
//Key: datatable field name (source data) - from a flat file (so no concerns about injection etc)
//Value: destination table field names
DataTable sourceData = ReadFromFile(@"\\server\shares\staff.csv");
StringBuilder tsql = new StringBuilder(1024);
tsql.AppendLine("insert into staffdata (");
foreach (var value in mapColumns.Values)
{
tsql.Append($"{value},");
}
tsql.Length--;
tsql.Append(") values");
foreach (DataRow row in sourceData.Rows)
{
tsql.AppendLine().Append("(");
foreach (string key in mapColumns.Keys)
{
tsql.Append($"'{row[key]}',");
}
tsql.Length--;
tsql.Append("),");
}
tsql.Length--;
InsertIntoDatabase(tsql.ToString());
}
我知道字典关键字值对的排序是不确定的,因此插入对的顺序不一定保持不变. 这一点都不是问题.
Is it therefore possible that the order of the pairs could get "shuffled" up between the Value iteration and then Key Iteration?个
我正试图完全理解词典迭代的未确定顺序是如何工作的
例如-源数据
IDCode FirstName LastName
A104 Joe Soap
Q187 Martin Smith
我期待(也很高兴):
EmployeeUID Name Surname
Q187 Martin Smith
A104 Joe Soap
然而,它是否可能插入为:(KeyValue对在值迭代后更改顺序)
EmployeeUID Name Surname
Soap Joe A104
Smith Martin Q187
我已经运行了自己的测试,没有发现它们会发生变化,但也许在1000000次迭代中我很幸运,或者我的测试在某些方面有缺陷