2009-12-04 3 views
2

두 번째 해시 테이블에서 업데이트하려는 해시 테이블이 있습니다. 일치하는 키가 있으면 그 값을 복사하고 싶습니다. 내가 가진 문제는 해시 테이블 키를 열거하고 각 문자열을 Guid를 String으로 형 변환하는 예외를받는 문자열로 캐스팅하려고 할 때입니다. 그게 내가 원하는 끈이야. index 연산자를 hashtable [ "FirstName"]과 같이 사용할 때 FirstName이 키가 될 것으로 기대합니다. 추측 아래 Guids를 사용할 수도 있지만 키의 문자열 인 키 값을 가져와야합니다.Hashtable 항목의 키를 얻는 방법

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

편집 나는이 해시 테이블 중 하나를 작성하지 않습니다. 필드 이름을 다음과 같이 넣고 필드 값을 가져올 수 있기 때문에 키에 문자열이 있습니다. 나는 모든 필드에 대해 다음을 수행하는 속기 방법으로 만들려고 노력 해요 : 해시 테이블은 GUID를 사용했다 됐어요

workflowProperties.Item["FirstName"] = infopathFields["FirstName"]; 
workflowProperties.Item["LastName"] = infopathFields["LastName"]; 
workflowProperties.Item["Address"] = infopathFields["Address"]; 
workflowProperties.Item["DOB"] = infopathFields["DOB"]; 
ect... 

편집을, 그러나 또한 분명히 그럴 것 다른 내부 문자열을 가지고 infopathFields [ "FirstName"]을 (를) 수행 할 수 있어야합니다. 그것은 내가 원하는 문자열을 전달하는 값입니다.

+0

나는 당신의 GUID를 문자열이 혼합지고있는 이유 모르겠지만, 왜 이전 Hasht를 사용하는 2.0 이상의 사전을 사용할 수 있습니까? –

+0

해시 테이블을 만들지 않습니다. –

+1

나는 당신을 도우려는 사람들을 모욕하는 것에 감사하지 않기 때문에이 표를 썼습니다. –

답변

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

내가 무엇을하고 있는지. 대답하기 전에 다른 시도가 실제로 질문을 읽었 으면 좋겠다. 건배 –

+0

여기에 매우 일반적인 문제가 있습니다 – harryovers

+0

그냥 참고로,이 질문에 따라 당신이 원하지 않는 GUID를 무시합니다. 문자열 문자열 fieldName = de.Key; 키의 유형이 Guid 인 경우 fieldName을 null로 유지합니다. 그런 다음 if 문은 false를 반환하고 Guid는 무시됩니다. 따라서 나는이 해결책이 맞다고 생각하지 않는다. –

0

Hashtable을 만드는 이유는 무엇입니까? 키는 실제로는 개체이므로 어떤 암시 적 문자열이라도 암시 적 캐스트가없는 것처럼 들립니다.

0

infopathFields 값 유형이 Guid 인 경우 workflowProperties 값의 유형은 Guids 여야합니다. 스 니펫에서 워크 플로 속성이 정의 된 것을 볼 수 없습니다.

는 가이 드 객체 그래서 당신은 당신이 키 열거에서 얻을 개체에 ToString()를 호출 할 필요가 문자열을 얻기 위해, 해시 테이블에 저장 Guid.ToString()

+0

나는이 선을 따라 무엇인가를 게시하려하고 있었다.거의 확실하게 Hashtable이 GUID를 키 값으로 사용하고 있습니다. –

0

객체를 사용하여 문자열로 GUID를 변환합니다. 나는 또한 런타임보다는 컴파일 타임에 이와 같은 문제를 잡을 것이므로 Hashtable 대신 generic Dictionary<K,V> 클래스를 사용할 것을 권장한다.

+0

해시 테이블을 만들지 않습니다. infopath 양식은 제출 될 때 해시 테이블을 출력하고 sharepoint는 필드에 액세스 할 수 있도록 목록의 각 항목에 대해 자체 해시 테이블을 제공합니다. –

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 만 전달됩니다. –

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]); 
     } 
    } 
} 
관련 문제