web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Can we add duplicates to dictionary?

(0) ShareShare
ReportReport
Posted on by 7,316

Hi,

I have a dictionary(Dictionary<string, Guid>) which does not allow duplicates to be added. Is there a way that we can add duplicates into it?

Here is my code snippet which fails if there is a key already in the dictionary.

Dictionary<string, Guid> dictLookupLocation = new Dictionary<string, Guid>();

                  
                   QueryExpression locationQuery = new QueryExpression("new_locations");
                   locationQuery.ColumnSet = new ColumnSet(true);
                   EntityCollection locationCollection = CRMHelper.GetCRMService().RetrieveMultiple(locationQuery);

                   foreach (Entity location in locationCollection.Entities)
                   {
                       string locationName = location.GetAttributeValue<string>("new_office");

                       if (!dictLookupLocation.ContainsKey(locationName))
                       {
                           dictLookupLocation .Add(locationName, location.Id);
                       }
                   }

Thanks for any help.

*This post is locked for comments

I have the same question (0)
  • Aileen Gusni Profile Picture
    44,524 on at
    RE: Can we add duplicates to dictionary?

    Hi Meeno

    The dictionary purpose is to create a pair (unique pair) between key and value so you cant use this for duplicate.

    You can use generic list, array, or nested dictionary key pair for this.

    Thanks

  • Suggested answer
    Dhaval mistry Profile Picture
    810 on at
    RE: Can we add duplicates to dictionary?

    Correcting myself  List<KeyValuePair<string, Guid>>()

  • Aiden Kaskela Profile Picture
    19,696 on at
    RE: Can we add duplicates to dictionary?

    KVP doesn't hold duplicates because each KVP is unrelated from the others - you wouldn't have duplicates you'd just have two Key Value Pairs. You'd still need a collection to hold them, even if it's just a list. Then to access the values you'd need to select all the KVP from the list based on the key of each pair. I'd use a dictionary myself.

  • Suggested answer
    Dhaval mistry Profile Picture
    810 on at
    RE: Can we add duplicates to dictionary?

    I would suggest use List<KeyValuePair<string, Guid>> instead of Dictionary<string, Guid>

    KeyValuePair can hold duplicate value.

  • Verified answer
    Aiden Kaskela Profile Picture
    19,696 on at
    RE: Can we add duplicates to dictionary?

    Hi,

    A dictionary in C# has one key which returns one value - no exceptions (unless the key doesn't exist, they you'll get an Exception. Haha). In your case, the key is a name and the Value is an Guid. If you need multiple values for one key, you can change the type of dictionary so the one value you're returning is a list object, of Guids. I updated your code:

    Dictionary<string, List<Guid>> dictLookupLocation = new Dictionary<string, List<Guid>>();
     
    QueryExpression locationQuery = new QueryExpression("new_locations");
    locationQuery.ColumnSet = new ColumnSet(true);
    EntityCollection locationCollection = CRMHelper.GetCRMService().RetrieveMultiple(locationQuery);
    
    foreach (Entity location in locationCollection.Entities)
    {
     string locationName = location.GetAttributeValue<string>("new_office");
     if (!dictLookupLocation.ContainsKey(locationName))
     {
       dictLookupLocation.Add(locationName, new List<Guid>()); 
     }
     dictLookupLocation[locationName].Add(location.Id);
    }
    


    When this runs, you'll have one dictionary entry for each location name, but if you get the Value for a key it will be a list of all IDs.

    Hope this helps! I'd appreciate if you would mark this as a Verified answer.

    Thanks,

      Aiden

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
Aric Levin - MVP Profile Picture

Aric Levin - MVP 2 Moderator

#3
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans