Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 931187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:27:36+00:00 2026-05-15T20:27:36+00:00

Is there a C# data structure to map keys to multiple values? I have

  • 0

Is there a C# data structure to map keys to multiple values? I have a collection of items that I want to key by name; however, the name is not unique. Hashtable and Dictionary only allow unique keys. Lookup seems close to what I want; however, it is not mutable.

Is there a built in data structure that I’m missing or do I need to build one myself?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T20:27:37+00:00Added an answer on May 15, 2026 at 8:27 pm

    What you’re looking for is a multimap.

    You may want to take a look at the answer to this question.

    You may also want to look into the C5 Generic Collection library, which is free and has an implementation of a multimap.

    If you feel like rolling your own, a simple place to start is a dictionary of lists:

    Dictionary<TKey,List<TValue>>
    

    However, you can’t add to such a dictionary the normal way. You have to first check if the key already exists, and if so, fetch the value (list) and add to it. Otherwise, you need to create the list and populate it with a value.

    If you are so inclined, I would suggest you consider using a set of extension methods to simplify the Add/Remove operations:

    public static class MultimapExt
    {
        public static void Add<TKey,TValue>( 
            this Dictionary<TKey,List<TValue>> dictionary, TKey key, TValue value )
        {
            List<TValue> valueList;
            if( !dictionary.TryGetValue( key, out valueList )
            {
                valueList = new List<TValue>();
                dictionary.Add( key, valueList );
            }
            valueList.Add( value );
        }
    
        public static void Remove<TKey,TValue>(
            this Dictionary<TKey,List<TValue>> dictionary, TKey key, TValue value )
        {
            List<TValue> valueList;
            if( dictionary.TryGetValue( key, out valueList ) )
            {
                valueList.Remove( value );
                if( valueList.Count == 0 )
                   dictionary.Remove( key ); 
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a map like data structure that uses Java Class objects as keys
Is there a map like data structure with integer as key , string as
Is there any good file based key->value data-structure available in c++. similar to std::map(template
Is there any way to get a map or other data structure of the
I have the data structure For each item there is a record of it's
Is there any tricky way to implement a set data structure (a collection of
I have a nested, mutual recursive data structure , and want to associate computational
Have data that has this kind of structure: $input = [ { animal: 'cat',
Is there a data structure in C++ with O(1) lookup? A std::map has O(log(n))
I have a map generated by others with data structure like this: x =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.