C# 如何获取哈希表项的键
我有一个哈希表,我想从第二个哈希表更新它。对于匹配的任何键,我都要复制该值。我遇到的问题是,当我枚举哈希表键并尝试将每个键强制转换为字符串时,我收到一个关于将Guid强制转换为字符串的异常。这就是我想要的绳子。当您将index操作符与hashtable[“FirstName”]一起使用时,我希望FirstName是键。我想它可能会在下面使用guid,但我需要找出键的字符串,键值C# 如何获取哈希表项的键,c#,hashtable,C#,Hashtable,我有一个哈希表,我想从第二个哈希表更新它。对于匹配的任何键,我都要复制该值。我遇到的问题是,当我枚举哈希表键并尝试将每个键强制转换为字符串时,我收到一个关于将Guid强制转换为字符串的异常。这就是我想要的绳子。当您将index操作符与hashtable[“FirstName”]一起使用时,我希望FirstName是键。我想它可能会在下面使用guid,但我需要找出键的字符串,键值 private void UpdateSharePointFromInfoPath(Hashtable infopat
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...
编辑
据说hashtable使用guid,但它显然也包含一个字符串,否则我将无法使用infopathFields[“FirstName”]。我想要的是传入的字符串上的值。是什么创建了哈希表?键实际上是一个对象,因此它听起来像是任何填充的对象,它没有隐式转换为字符串如果infopathFields的值类型是Guid,则workflowProperties的值类型必须是Guid。我无法从代码片段中看到workflowProperties的定义
要将Guid转换为字符串,请使用哈希表中存储的对象是Guid对象,因此要获取字符串,需要对从键枚举器获取的对象调用
ToString()
。我还建议使用genericDictionary
类而不是Hashtable,因为这样会在编译时而不是运行时发现类似的问题。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();
}
每个项都是格式字典entry的键/值对
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();
要从哈希表中获取最大整数键,请执行以下操作:
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]);
}
}
}
我不知道为什么你的guid和字符串会混淆,但为什么你要使用旧的Hashtable类而不是2.0及以上版本的字典?我不创建Hashtable。我投票否决了这一点,因为我不喜欢你侮辱那些试图帮助你的人。我正准备发布类似的东西。几乎可以肯定的是,您的哈希表使用GUID作为键值。我不创建哈希表。infopath表单在提交时输出哈希表,sharepoint为列表中的每个项目公开其自己的哈希表,以便您可以访问字段。如果来自.Keys的对象是Guid,则将其强制转换为字符串只会为我提供字符串格式的Guid。这正是我想要的。我希望其他人在回答问题之前能仔细阅读问题。干杯仅供参考,这将忽略GUI,根据你的问题,这是你不想做的。行字符串fieldName=de.Key作为字符串;当键的类型为Guid时,字段名将为null。然后if语句返回false并忽略Guid。因此,我认为这个解决方案是不正确的。