0

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); 
5
  • What have you tried? What are you having difficulties with? Please post the code you have written so far and explain where you are having issues. Commented Dec 16, 2011 at 9:37
  • I am confused to start. I tried foreach loop it will not work to manipulate both the table in single iteration. Commented Dec 16, 2011 at 9:40
  • Search Google ... lot example is out there. One here for you c-sharpcorner.com/Forums/Thread/83420/… Commented Dec 16, 2011 at 9:41
  • I don't understand what you're trying to do. What does 'compare values based on the key' mean? And what do you do with matching values? Commented Dec 16, 2011 at 10:04
  • Looks like this is misuse of a hashtable, all should be in an other way - you should find a value by a key not akey by a value. Commented Dec 16, 2011 at 10:12

4 Answers 4

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? } } 
Sign up to request clarification or add additional context in comments.

Comments

0

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; } } 

1 Comment

I am trying to compare values. See the question. If values are same then I want to get the keys. Means I want only those keys from the hash tables for which values are same...
0

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 

1 Comment

The intersect solution is not optimal since you already have a dictionary and the intersect would need to put all the keys into an internal Set.
0

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.

2 Comments

HashTable.Keys property has the type ICollection in System.Collections namespace. This does not have Intersect extension method (which is an extension for IEnumerable<TSource>).
@DanKodi You are correct, because HashTable doesn't have a strongly typed key, like Dictionary<TKey,TValue>. A call to Cast<string>() on the Keys collection will solve this though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.