I have a need for a dictionary with multiple keys of 2 different types (int and string, both unique, so they can appear only inside of 1 key). Here is an example: group information (GroupInfo) can be queried by either GroupdId or one of the member names:
GroupId MemberNames GroupInfo
{1, John, Mary, Joe} ==> {GroupInfo}
So group info should be returned when requested by either id (1) or one of the member names (John).
My first solution was to create a key that wraps GroupdId and MemberNames with overridden Equals method that compares GroupIds and looks up a list of members. However to make these entries equal:
GroupId MemberNames
{0, John}
{1, null}
{1, Mary}
GetHashCode has to return the same const value. This will result in a dictionary becoming a linked list and performance degrading to O(N) lookup in the best case scenario.
The other solution is to keep 2 dictionaries separately: GroupId ==> GroupInfo, MemberName ==> GroupInfo.
Any other ideas?
In order to maintain only 1 dictionary, consider converting the GroupId (int) into a string and use it as a key (number ‘keys’ should not conflict with name keys). Maintain a references to the keys, so that if one gets deleted, the rest will be deleted.