I query a hierarchical data source like this:
repository.LookupValue(IEnumerable<string> ascendants)
where ascendants is a list of strings representing the path to the node being looked up.
I would like to cache results from these lookups. I had envisioned using a Dictionary<IEnumerable<string>, long>, but it seems when using an IEnumerable<string> as a dictionary key, reference equality is used, which is no good to me.
I could of course define my own type wrapping IEnumerable<string> and override it’s .Equals method, but that seems extreme.
Am I missing a simpler solution to the caching problem? Can anyone advise?
Use a
Dictionarybut use the constructor that takes anIEqualityComparer. Write a custom implementation of this to compare your collection of ascendants and pass it in.Here’s an example (comparing arrays in this case) that uses built in
StructuralComparisons.