问题
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);
    回答1:
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?
            }
        }
    回答2:
Assuming that you are trying to match keys in both HashTable ... you can do like
 foreach (string key in oldtable.Keys)
    {
        if (newtable.Contains(key))
        {
            // do your work;
        }
        else
        {
            // do your work;         
       }
    }
    回答3:
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).
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()
var items = map1.Keys.Intersect(map2.Keys)
                     .Select(k => map1[k] + " / " + map2[k])
                     .ToList();
<.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
    回答4:
You could use Linq to intersect the keys giving you a collection of keys in both tables?
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.
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>
If you were using a strongly typed Dictionary<string,string> you would not need the Cast<string>() part.
来源:https://stackoverflow.com/questions/8532225/compare-values-of-two-hash-tables-in-loop