如何获取Hashtable条目的键

4
我有一个哈希表,想要从第二个哈希表中更新它。对于任何匹配的键,我想要复制值。但是,当我枚举哈希表键并尝试将每个键强制转换为字符串时,我会收到有关将 Guid 强制转换为 String 的异常。其实我需要的是字符串。当您使用类似 hashtable["FirstName"] 的索引运算符时,我期望 FirstName 是键。它可能在底层使用 Guids,但我需要获取键的字符串,即键值。
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...

编辑 有人说哈希表使用 Guids,但它显然也包含一个字符串,否则我就不能使用 infopathFields["FirstName"]。 我想要的是我传递进去的字符串的值。


我不确定为什么你的Guids和字符串会混淆,但是为什么你要使用旧的Hashtable类而不是2.0及以上版本的Dictionary呢? - Tim Robinson
我不创建哈希表。 - Daniel Revell
1
我投票反对此事,因为我不欣赏您侮辱那些试图帮助您的人。 - Jonathan Beerhalter
6个回答

9

每个条目都是一个键/值对,格式为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();

正是我想要的。希望其他一些尝试回答之前真正阅读问题。干杯 - Daniel Revell
只是提醒一下,这将忽略GUID,根据您的问题,您不想这样做。当键的类型为Guid时,行字符串fieldName = de.Key as string; 将使fieldName为空。然后if语句返回false并忽略Guid。因此,我认为这个解决方案是不正确的。 - Jonathan Beerhalter

1

Hashtable 的标准版本可以具有不同类型的键,因此您的大多数键可能是字符串,但某些键可能是 GUID。我敢打赌这就是问题所在。以下小型控制台应用程序演示了这个问题。

    static void Main(string[] args)
    {
        System.Collections.Hashtable htable = new System.Collections.Hashtable();
        htable.Add("MyName", "WindyCityEagle");
        htable.Add("MyAddress", "Here");
        htable.Add(new Guid(), "That Was My Guid");

        int loopCount = 0;
        foreach (string s in htable.Keys)
        {
            Console.WriteLine(loopCount++.ToString());
            Console.WriteLine(htable[s]);
        }
    }

您将会得到与您在这里报告的完全相同的异常。

我建议解决这个问题的方法是采用以下方法

private void UpdateSharePointFromInfoPath(Hashtable infopathFields)
{
    // Go through all the fields on the infopath form
    // Invalid Cast Exception Here
    foreach (object key in infopathFields.Keys)
    {

        string wfpKey = key.ToString();
        // If the same field is on sharepoint    
        if (workflowProperties.Item.Fields.ContainsField(wfpKey))
        {
            // Update the sharepoint field with the new value from infopath
            workflowProperties.Item[wfpKey] = infopathFields[key];
        }
    }
    // Commit the changes
    workflowProperties.Item.Update();
}

如果从.Keys中出来的对象是Guid,则将其转换为字符串只会给我一个字符串格式的Guid。 - Daniel Revell

0

从哈希表中获取最大的整数键:

public class Example
{
    public void hashTableMethod()
    {
        Hashtable ht = new Hashtable();
        ht.Add(5002894, "Hemant Kumar");
        ht.Add(5002895, "Himanshee Ratnakar");
        ht.Add(5002896, "Pooja Bhatnagar");
        ht.Add(5002897, "Hina Saxena");
        ht.Add(5002898, "Kanika Aneja");
        ht.Add(5002899, "Hitesh Chaudhary");

        Console.Write("\nNumber of Key-Value pair elements in HashTable are : {0}",ht.Count);
        Console.WriteLine("Elements in HashTable are: ");
        ICollection htkey = ht.Keys;
        foreach (int key in htkey)
        {
            Console.WriteLine("{0}. {1}",key,ht[key]);
        }
        string ch="n";
        do
        {
            Console.Write("\n\nEnter the name to check if it is exist or not, if not then it will add: ");
            string newName=Console.ReadLine();
            if(ht.ContainsValue(newName))
            {
                Console.Write("\nYour Name already Exist in the list!!");
            }
            else
            {
                Console.Write("\nSorry that name doesn't exist but it will be added!!");
                int getKey = 0;

                int[] htk= new int[ht.Count];
                ht.Keys.CopyTo(htk,0);

                string[] val=new string[ht.Count];
                ht.Values.CopyTo(val,0);

                Array.Sort(htk,val);
                foreach (int id in htk)
                {
                    getKey = id;  
                }
                ht.Add(getKey+1,newName);
            }
            Console.Write("\nDo you want to search more??(y/n) :");
            ch=Console.ReadLine();
        }while(ch=="y"||ch=="Y");

        Console.Write("\nNew List Items: \n");
        ICollection htkeys = ht.Keys;
        foreach (int key in htkeys)
        {
            Console.WriteLine("{0}. {1}",key,ht[key]);
        }
    }
}

0

什么创建了Hashtable?键实际上是一个对象,因此似乎填充它的任何内容都没有隐式转换为字符串。


0
如果 infopathFields 的值类型是 Guid,则 workflowProperties 的值类型也必须是 Guid。从代码片段中无法看出 workflowProperties 的定义。
要将 Guid 转换为字符串,请使用 Guid.ToString()

我正要发布一些类似的内容。几乎可以确定你的Hashtable正在使用GUID作为键值 - Crowe T. Robot

0
哈希表中存储的对象是Guid对象,因此要获取字符串,您需要在从键枚举器获取的对象上调用ToString()。我还建议使用通用的Dictionary<K,V>类,而不是Hashtable,因为这样可以在编译时而不是运行时捕获此类问题。

我不创建哈希表。当提交Infopath表单时,它会输出哈希表,而SharePoint为列表上的每个项目公开自己的哈希表,因此您可以访问字段。 - Daniel Revell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接