2009-12-04 226 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]); 
     } 
    } 
}