【问题标题】:How to get the key of a Hashtable entry如何获取 Hashtable 条目的键
【发布时间】:2009-12-04 16:59:53
【问题描述】:

我有一个要从第二个哈希表更新的哈希表。对于任何匹配的键,我想将值复制过来。我遇到的问题是,当我枚举哈希表键并尝试将每个键转换为字符串时,我收到一个关于将 Guid 转换为字符串的异常。嗯,这是我想要的字符串。当您将索引运算符与 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”]。这是我想要传入的字符串上的值。

【问题讨论】:

  • 我不确定为什么您的 Guid 和字符串会混淆,但您为什么使用旧的 Hashtable 类而不是 2.0 及以上版本的字典?
  • 我不创建哈希表。
  • 我投了反对票,因为我不欣赏你侮辱那些试图帮助你的人。

标签: c# hashtable


【解决方案1】:

每一项都是字典条目格式的键/值对

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();

【讨论】:

  • 正是我所追求的。希望其他一些尝试在回答之前实际上已经阅读了这个问题。干杯
  • 仅供参考,这将忽略 GUID,根据您的问题,您不想这样做。行字符串 fieldName = de.Key as string;当键的类型为 Guid 时,fieldName 将为 null。然后 if 语句返回 false 并忽略 Guid。因此我认为这个解决方案是不正确的。
【解决方案2】:

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。
【解决方案3】:

什么创建了哈希表?键实际上是一个对象,所以听起来就像填充它的任何内容都没有隐式转换为字符串

【讨论】:

    【解决方案4】:

    如果 infopathFields 的值的类型是 Guid,那么 workflowProperties 的值的类型必须是 Guid。我无法从 sn-p 中看到 workflowProperties 的定义是什么。

    要将 Guid 转换为字符串,请使用 Guid.ToString()

    【讨论】:

    • 我正要按照这些思路发布一些内容。几乎可以肯定,您的 Hashtable 使用 GUID 作为键值
    【解决方案5】:

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

    【讨论】:

    • 我不创建哈希表。信息路径表单在提交时输出哈希表,sharepoint 为列表中的每个项目公开它自己的哈希表,以便您可以访问这些字段。
    【解决方案6】:

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

    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]);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-15
      • 2020-02-20
      • 2021-07-28
      • 1970-01-01
      • 2011-02-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多