[英]Compare values of two hash tables in loop
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);
Is this the way you want it? 这是您想要的方式吗?
Hashtable OldTable = new Hashtable();
Hashtable NewTable = new Hashtable();
foreach (DictionaryEntry entry in OldTable)
{
if(NewTable.ContainsKey(entry.Key))
{
//Do something?
}
}
You could use Linq to intersect the keys giving you a collection of keys in both tables? 您可以使用Linq交叉键,从而在两个表中都提供键集合吗?
var combinedKeys=OldTable.Keys.Cast<string>().Intersect(NewTable.Keys.Cast<string>())
you can then iterate over the keys or use a Linq Select or Agregate statement to get a collection result. 然后,您可以遍历键或使用Linq Select或Agregate语句获取收集结果。
EDIT 编辑
Because HashTable is not strongly typed, Keys
does not give you an IEnumerable, hence the call to Cast<string>()
to get an IEnumerable<string>
由于HashTable的类型不是严格的,因此Keys
不会给您IEnumerable,因此调用Cast<string>()
以获得IEnumerable<string>
If you were using a strongly typed Dictionary<string,string>
you would not need the Cast<string>()
part. 如果使用的是强类型的Dictionary<string,string>
,则不需要Cast<string>()
部分。
Assuming that you are trying to match keys in both HashTable ... you can do like 假设您要匹配两个HashTable中的键...您可以这样做
foreach (string key in oldtable.Keys)
{
if (newtable.Contains(key))
{
// do your work;
}
else
{
// do your work;
}
}
First thing - use typed Dictionary<TKey, TValue>
rather than Hashtable
, below is example when values are of string
type, this would be easy changing string
to your custom type (I believe enum
or already string constants). 第一件事-使用类型化的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 and upper : Usign LINQ Intersect()
.NET 3.5及更高版本 :Usign LINQ Intersect()
var items = map1.Keys.Intersect(map2.Keys)
.Select(k => map1[k] + " / " + map2[k])
.ToList();
<.NET 3.5 <.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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.