如何递归搜索并比较动态创建的嵌套Hashtable?
Hey there! Let's tackle this nested Hashtable comparison problem you're working on. First off, no worries about not being able to perfectly还原 the dynamic structure—your access pattern gives us enough to work with. Let's start by mapping out what your Hashtable likely looks like based on that, then dive into how to compare it with another identical-structure (but possibly different-value) nested Hashtable.
Based on your note that PDH083 is a dynamically generated key tied to a booking number, here's a concrete example of what your structure might resemble (using C# syntax since Hashtable is common in .NET):
// Main Hashtable containing dynamic booking keys Hashtable bookingHash = new Hashtable(); // Nested Hashtable for a specific booking (PDH083) Hashtable pdh083Details = new Hashtable(); pdh083Details.Add("BookingDate", new DateTime(2024, 5, 15)); pdh083Details.Add("TotalAmount", 199.99); pdh083Details.Add("Status", "Confirmed"); // Add the nested table to the main one with the dynamic key bookingHash.Add("PDH083", pdh083Details);
Since your goal is to check for value differences while accounting for the dynamic, nested structure, here's a step-by-step approach with code examples:
1. First Validate Structural Consistency
Before checking values, make sure both Hashtables have matching structures:
- Verify that all dynamic keys (like
PDH083) exist in both main Hashtables - For each dynamic key, confirm the nested Hashtables have identical sets of sub-keys
- If structures don't match, you'll want to flag that as a discrepancy first
2. Recursive Comparison Method
Nested structures call for recursive logic to dig through every level. Here's a reusable method to compare two nested Hashtables:
public bool AreNestedHashtablesEqual(Hashtable hashA, Hashtable hashB) { // First check if key counts match if (hashA.Count != hashB.Count) return false; foreach (DictionaryEntry entry in hashA) { string currentKey = entry.Key.ToString(); // Check if the key exists in the second Hashtable if (!hashB.ContainsKey(currentKey)) return false; var valueA = entry.Value; var valueB = hashB[currentKey]; // If both values are Hashtables, recurse into them if (valueA is Hashtable nestedA && valueB is Hashtable nestedB) { if (!AreNestedHashtablesEqual(nestedA, nestedB)) return false; } // Otherwise, compare the values directly else if (!Equals(valueA, valueB)) { return false; } } return true; }
3. Get Specific Difference Details (Optional)
If you need to know exactly what's different (not just whether they're equal), modify the logic to track discrepancies:
public List<string> GetHashtableDifferences(Hashtable hashA, Hashtable hashB, string currentPath = "") { List<string> differences = new List<string>(); foreach (DictionaryEntry entry in hashA) { string key = entry.Key.ToString(); string newPath = string.IsNullOrEmpty(currentPath) ? key : $"{currentPath} -> {key}"; if (!hashB.ContainsKey(key)) { differences.Add($"Key missing in second Hashtable: {newPath}"); continue; } var valueA = entry.Value; var valueB = hashB[key]; if (valueA is Hashtable nestedA && valueB is Hashtable nestedB) { differences.AddRange(GetHashtableDifferences(nestedA, nestedB, newPath)); } else if (!Equals(valueA, valueB)) { differences.Add($"Value mismatch at {newPath}: '{valueA}' vs '{valueB}'"); } } // Check for keys present in hashB but not hashA foreach (DictionaryEntry entry in hashB) { string key = entry.Key.ToString(); if (!hashA.ContainsKey(key)) { string newPath = string.IsNullOrEmpty(currentPath) ? key : $"{currentPath} -> {key}"; differences.Add($"Key missing in first Hashtable: {newPath}"); } } return differences; }
4. Handle Dynamic Key Edge Cases
- If your dynamic keys are generated from the same booking data but might have formatting differences (e.g.,
pdh083vsPDH083), add a normalization step (like converting to uppercase) before checking key existence - If you need to compare bookings across Hashtables even when keys don't match (e.g., different identifiers for the same booking), add logic to map keys using shared booking attributes (like a booking ID stored in the nested Hashtable)
内容的提问来源于stack exchange,提问作者Spacey

