2009-12-04 237 views
2

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

编辑 有人说,该哈希表使用的GUID,但它也显然有其他内部字符串我不会能够做到infopathFields [“FirstName”]。这是我想要的字符串中的值。

+0

我不知道为什么你的Guids和字符串混在一起,但你为什么要用旧的Hasht能够上课而不是2.0以上的词典? – 2009-12-04 17:06:49

+0

我不创建哈希表。 – 2009-12-04 17:22:43

+1

我投了弃权票,因为我不赞赏你侮辱那些试图帮助你的人。 – 2009-12-04 18:07:57

回答

7

每个产品格式的键/值对的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(); 
+0

正是我所追求的。希望其他一些尝试在回答之前实际上已经阅读了这个问题。欢呼 – 2009-12-04 17:41:40

+0

在这里很常见的问题 – harryovers 2009-12-04 17:42:19

+0

只是一个FYI,这将忽略GUID,根据你的问题,你不想这样做。行字符串fieldName = de.Key as string;当键的类型为Guid时,将使fieldName为null。然后if语句返回false并忽略Guid。所以我不认为这个解决方案是正确的。 – 2009-12-04 18:06:29

0

什么创建了Hashtable?关键是实际上是一个对象,所以它听起来像任何填充它没有任何隐含的投影到字符串

0

如果infopathFields的值的类型是Guid那么workflowProperties的值的类型将必须是Guids。我无法从代码片段中看到workflowProperties定义为什么。

为GUID转换为字符串使用Guid.ToString()

+0

我刚刚发布了一些内容。几乎肯定你的Hashtable使用GUID作为键值 – 2009-12-04 17:08:53

0

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

+0

我不创建哈希表。 infopath表单在提交时输出哈希表,并且Sharepoint公开它自己的哈希表以显示列表上的每个项目,以便您可以访问这些字段。 – 2009-12-04 17:16:34

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(); 
} 
+0

如果从.Keys出来的对象是一个Guid,那么将它转换为一个字符串只会给我字符串格式的Guid。 – 2009-12-04 17:38:36

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