I have situation where in the Repository class I have a dictionary:

Dictionary<TableName, Dictionary<EntityColumnName, SourceColumnName> map1 = new Dictionary<TableName, Dictionary<EntityColumnName, SourceColumnName> ()

with methods for manipulating data (Get,Save,Add etc).

Now there is a need to search by value from the inner map, not only by key (in this example SourceColumnName).

My question is what would be best approach here? I want to add another dictionary but with reversed data:

Dictionary<TableName, Dictionary<SourceColumnName, EntityColumnName> map2 = new Dictionary<TableName, Dictionary<SourceColumnName, EntityColumnName> ()

But the problem is memory usage, is that a waste of memory? But if I implement lookup by value, I'll lose the main advantage of dictionary.

The data stored in the dictionary is around 20 * 5k data in the inner map. Any suggestion how to design / refactor this class would be appreciated.

EDIT1: Micro optimisation post didn't resolved my problem. I work on project where optimisation is very important.


The type you're looking for is generally called a 'bidirectional dictionary'; I found a C# implementation in this Stack Overflow answer, but you're on the right track; you'll need two dictionaries, so memory usage will be higher. Whether this is worth the increase in performance you can only find out by testing and measuring.

| improve this answer | |
  • Firstly thanks for answer. This means that 2 dictionary is good option and i will accept this answer. – NSKBpro Feb 6 '19 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.