循环中比较两个哈希表的值
我有两个哈希表。我想根据键比较两个哈希表的值。我想在循环中执行此操作,如果找到匹配则想要执行字符串构建操作。但问题是我不知道有什么机制可以在循环中比较它们。请指导我... 以下是我要比较的哈希表
HashTable OldTable= new HashTable();
OldTable.Add("Date of Event", OCEFData.EventDate);
OldTable.Add("Angina Status", OCEFData.AnginaStatusValue);
OldTable.Add("Please indicate the body system involved (tick all that apply)",strBodySystem.ToString());
OldTable.Add("If Unstable Angina, define Braunswald Classification", OCEFData.UnstableAnginaValue);
OldTable.Add("If Stable Angina", OCEFData.StableAnginaValue);
OldTable.Add("Details of method of Documentation of Angina", OCEFData.AnginaDocDetails);
OldTable.Add("INFORM TO SPONSOR", (OCEFData.IsInformed)?"Yes":"No");
OldTable.Add("DATE OF INFORMATION TO SPONSOR ", OCEFData.SponsorDate);
OldTable.Add("DATE OF INFORMATION TO INSTITUTIONAL ETHICS COMMITTEE", OCEFData.EthicsCommitteeDate);
OldTable.Add("DATE OF INFORMATION TO LICENSING AUTHORITY", OCEFData.LicensingAuthority);
HashTable NewTable= new HashTable();
NewTable.Add("Date of Event", OCEFData.EventDate);
NewTable.Add("Angina Status", OCEFData.AnginaStatusValue);
NewTable.Add("Please indicate the body system involved (tick all that apply)", strBodySystem.ToString());
NewTable.Add("If Unstable Angina, define Braunswald Classification", OCEFData.UnstableAnginaValue);
NewTable.Add("If Stable Angina", OCEFData.StableAnginaValue);
NewTable.Add("Details of method of Documentation of Angina", OCEFData.AnginaDocDetails);
NewTable.Add("INFORM TO SPONSOR", (OCEFData.IsInformed)?"Yes":"No");
NewTable.Add("DATE OF INFORMATION TO SPONSOR ", OCEFData.SponsorDate);
NewTable.Add("DATE OF INFORMATION TO INSTITUTIONAL ETHICS COMMITTEE", OCEFData.EthicsCommitteeDate);
NewTable.Add("DATE OF INFORMATION TO LICENSING AUTHORITY", OCEFData.LicensingAuthority);
I have two hash tables. I want to compare values of both the hash tables based on the key. I want to do this in loop and if match is found is want to perform string building operation. But the problem is I dont know any mechanism to compare them in loop. Please guide me...
Following are my hash tables to be compared
HashTable OldTable= new HashTable();
OldTable.Add("Date of Event", OCEFData.EventDate);
OldTable.Add("Angina Status", OCEFData.AnginaStatusValue);
OldTable.Add("Please indicate the body system involved (tick all that apply)",strBodySystem.ToString());
OldTable.Add("If Unstable Angina, define Braunswald Classification", OCEFData.UnstableAnginaValue);
OldTable.Add("If Stable Angina", OCEFData.StableAnginaValue);
OldTable.Add("Details of method of Documentation of Angina", OCEFData.AnginaDocDetails);
OldTable.Add("INFORM TO SPONSOR", (OCEFData.IsInformed)?"Yes":"No");
OldTable.Add("DATE OF INFORMATION TO SPONSOR ", OCEFData.SponsorDate);
OldTable.Add("DATE OF INFORMATION TO INSTITUTIONAL ETHICS COMMITTEE", OCEFData.EthicsCommitteeDate);
OldTable.Add("DATE OF INFORMATION TO LICENSING AUTHORITY", OCEFData.LicensingAuthority);
HashTable NewTable= new HashTable();
NewTable.Add("Date of Event", OCEFData.EventDate);
NewTable.Add("Angina Status", OCEFData.AnginaStatusValue);
NewTable.Add("Please indicate the body system involved (tick all that apply)", strBodySystem.ToString());
NewTable.Add("If Unstable Angina, define Braunswald Classification", OCEFData.UnstableAnginaValue);
NewTable.Add("If Stable Angina", OCEFData.StableAnginaValue);
NewTable.Add("Details of method of Documentation of Angina", OCEFData.AnginaDocDetails);
NewTable.Add("INFORM TO SPONSOR", (OCEFData.IsInformed)?"Yes":"No");
NewTable.Add("DATE OF INFORMATION TO SPONSOR ", OCEFData.SponsorDate);
NewTable.Add("DATE OF INFORMATION TO INSTITUTIONAL ETHICS COMMITTEE", OCEFData.EthicsCommitteeDate);
NewTable.Add("DATE OF INFORMATION TO LICENSING AUTHORITY", OCEFData.LicensingAuthority);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是你想要的方式吗?
Is this the way you want it?
假设您正在尝试匹配两个 HashTable 中的键...您可以这样做
Assuming that you are trying to match keys in both HashTable ... you can do like
首先 - 使用类型化的
Dictionary
而不是Hashtable
,下面是值是string
类型时的示例,这很容易将string
更改为您的自定义类型(我相信enum
或已经是字符串常量)。.NET 3.5 及更高版本:Usign LINQ
Intersect()
<.NET 3.5
First thing - use typed
Dictionary<TKey, TValue>
rather thanHashtable
, below is example when values are ofstring
type, this would be easy changingstring
to your custom type (I believeenum
or already string constants)..NET 3.5 and upper: Usign LINQ
Intersect()
<.NET 3.5
您可以使用 Linq 来交叉键,从而获得两个表中的键集合?
然后,您可以迭代这些键或使用 Linq Select 或 Agregate 语句来获取集合结果。
编辑
由于 HashTable 不是强类型,
Keys
不会为您提供 IEnumerable,因此调用Cast()
来获取IEnumerable
如果您使用强类型
Dictionary
您将不需要Cast()
部分。You could use Linq to intersect the keys giving you a collection of keys in both tables?
you can then iterate over the keys or use a Linq Select or Agregate statement to get a collection result.
EDIT
Because HashTable is not strongly typed,
Keys
does not give you an IEnumerable, hence the call toCast<string>()
to get anIEnumerable<string>
If you were using a strongly typed
Dictionary<string,string>
you would not need theCast<string>()
part.