我有一个哈希表,我想从第二个哈希表更新它。对于任何匹配的键,我想复制值。我遇到的问题是,当我枚举哈希表键并尝试将每个键转换为字符串时,我收到有关将 Guid 转换为字符串的异常。好吧,这就是我想要的字符串。当您将索引运算符与 hashtable["FirstName"] 之类的东西一起使用时,我希望 FirstName 成为关键。我猜它可能在下面使用 Guid,但我需要取出键的字符串,键值。
private void UpdateSharePointFromInfoPath(Hashtable infopathFields)
{
// Go through all the fields on the infopath form
// Invalid Cast Exception Here
foreach (String fieldName in infopathFields.Keys)
{
// If the same field is on sharepoint
if (workflowProperties.Item.Fields.ContainsField(fieldName))
{
// Update the sharepoint field with the new value from infopath
workflowProperties.Item[fieldName] = infopathFields[fieldName];
}
}
// Commit the changes
workflowProperties.Item.Update();
}
编辑 我不创建这些哈希表中的任何一个。键在某处有字符串,因为我可以像下面这样输入字段名称并获取字段的值。我正在尝试为每个字段做以下操作的速记方法:
workflowProperties.Item["FirstName"] = infopathFields["FirstName"];
workflowProperties.Item["LastName"] = infopathFields["LastName"];
workflowProperties.Item["Address"] = infopathFields["Address"];
workflowProperties.Item["DOB"] = infopathFields["DOB"];
ect...
编辑 据说哈希表使用 Guid,但它显然也有一个字符串,否则我将无法执行 infopathFields["FirstName"]。这是我传递给那里的字符串的值。
最佳答案
每个项目都是格式为 DictionaryEntry 的键/值对
foreach (DictionaryEntry de in infopathFields)
{
string fieldName = de.Key as string;
if (workflowProperties.Item.Fields.ContainsField(fieldName))
{
workflowProperties.Item[fieldName] = infopathFields[fieldName];
}
}
workflowProperties.Item.Update();
关于c# - 如何获取哈希表条目的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1848299/