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

Announcements

No record found.

News and Announcements icon
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)
  • Verified answer
    Aiden Kaskela Profile Picture
    19,696 on at

    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

  • Suggested answer
    Dhaval mistry Profile Picture
    810 on at

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

    KeyValuePair can hold duplicate value.

  • Aiden Kaskela Profile Picture
    19,696 on at

    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

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

  • Aileen Gusni Profile Picture
    44,524 on at

    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

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans