c# - 如何获取哈希表条目的键

标签 c# hashtable

我有一个哈希表,我想从第二个哈希表更新它。对于任何匹配的键,我想复制值。我遇到的问题是,当我枚举哈希表键并尝试将每个键转换为字符串时,我收到有关将 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/

相关文章:

c# - 使用 WPF 动态加载内容

c# - 如何将强名称公钥 (snk) 转换为 <RSAKeyValue>?

c# - 哪种数据访问技术最适合 CQRS 查询 Web 服务?

c# - 在 Controller 中存储和访问对象列表的最佳方式

c++ - 如何找到哈希表的大小?

c# - TabControl.DrawItem 未在用户绘制的 TabControl 上触发

data-structures - 发生冲突时,哈希表如何读取正确的值?

c++ - unordered_map<TYPE, bool> 与 set<TYPE>

为 hashmap 创建可变数量的链表

C - 对 void 类型进行哈希处理?