我有两个哈希表。我想基于密钥比较两个哈希表的值。我想在循环中执行此操作,如果找到匹配则要执行字符串构建操作。但问题是我不知道在循环中比较它们的任何机制。请指导我...... 以下是我要比较的哈希表
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);
答案 0 :(得分:1)
这是你想要的吗?
Hashtable OldTable = new Hashtable();
Hashtable NewTable = new Hashtable();
foreach (DictionaryEntry entry in OldTable)
{
if(NewTable.ContainsKey(entry.Key))
{
//Do something?
}
}
答案 1 :(得分:0)
您可以使用Linq交叉键,在两个表中为您提供键集合吗?
var combinedKeys=OldTable.Keys.Cast<string>().Intersect(NewTable.Keys.Cast<string>())
然后,您可以迭代键或使用Linq Select或Agregate语句来获取收集结果。
修改强>
由于HashTable没有强类型,Keys
没有为您提供IEnumerable,因此调用Cast<string>()
获取IEnumerable<string>
如果您使用的是强类型Dictionary<string,string>
,则不需要Cast<string>()
部分。
答案 2 :(得分:0)
假设您正在尝试匹配两个HashTable中的键...您可以这样做
foreach (string key in oldtable.Keys)
{
if (newtable.Contains(key))
{
// do your work;
}
else
{
// do your work;
}
}
答案 3 :(得分:0)
首先 - 使用键入的Dictionary<TKey, TValue>
而不是Hashtable
,下面是值为string
类型的示例,这可以很容易地将string
更改为您的自定义类型(我相信enum
或已经是字符串常量。)
IDictionary<string, string> map1 = new Dictionary<string, string>
{
{"A", "M1-A"},
{"B", "M1-B"},
{"C", "M1-C"}
};
IDictionary<string, string> map2 = new Dictionary<string, string>
{
{"A", "M2-A"},
{"B", "M2-B"},
{"D", "M2-D"}
};
.NET 3.5及更高版本:使用LINQ Intersect()
var items = map1.Keys.Intersect(map2.Keys)
.Select(k => map1[k] + " / " + map2[k])
.ToList();
&lt; .NET 3.5
IList<string> results = new List<string>();
foreach (var key in map1.Keys)
{
if (map2.ContainsKey(key))
{
results.Add(map1[key] + " / " + map2[key]);
}
}
OUTPUT:
M1-A / M2-A
M1-B / M2-B