我有一个哈希表,我想从第二个哈希表更新。对于任何匹配的键,我想复制该值。我遇到的问题是当我枚举哈希表键并尝试将每个键转换为字符串时,我收到一个关于将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”]。这是我在那里传递的字符串的值。
答案 0 :(得分: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();
答案 1 :(得分: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();
}
答案 2 :(得分:0)
创建Hashtable的原因是什么?键实际上是一个对象,所以它听起来像填充它没有隐式强制转换为字符串
答案 3 :(得分:0)
如果infopathFields的值类型是Guid,那么workflowProperties的值类型必须是Guids。我无法从代码段中看到workflowProperties定义为什么。
要将Guid转换为字符串,请使用Guid.ToString()
答案 4 :(得分:0)
存储在散列表中的对象是Guid对象,因此要获取字符串,您需要在从密钥枚举器获取的对象上调用ToString()
。我还建议使用泛型Dictionary<K,V>
类而不是Hashtable,因为它会在编译时而不是运行时捕获这样的问题。
答案 5 :(得分: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]);
}
}
}