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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

CRM development: Entity array to dictionary

(0) ShareShare
ReportReport
Posted on by

Looking to add our subject entity parentsubjects to a dictionary. I have been able to create the array as follows, with the ‘title’ required appearing in Attributes > Values within each subject selected:

 

var query = new QueryExpression()

{

EntityName = Subject.EntityLogicalName,

ColumnSet = new ColumnSet("title"),

Criteria = new FilterExpression()

};

query.Criteria.AddCondition("parentsubject", ConditionOperator.Null);

query.ColumnSet.AddColumns("title");

 

var subjectRecords = myCRMProxy.RetrieveMultiple(query).Entities.ToArray();

Entity[] allSubjects = subjectRecords;


Previously we have added our optionsets to a dictionary using the code below – the OptionMetadata class has the Value / Label methods needed but I’ve been unable to find a similar class that invokes these against the entity;

 

foreach (OptionMetadata optionMetadata in optionList)

               {

                   dictionary.Add((Int64)optionMetadata.Value, optionMetadata.Label.LocalizedLabels[0].Label);

               }

 

How can we add the entity array Values to our dictionary? Any help immensely appreciated.

*This post is locked for comments

I have the same question (0)
  • Bas van de Sande Profile Picture
    4,383 on at

    Technically:

    You could use the Dictionary<TKEY,TVAL> of the System.Collections.Generic namespace to facilitate your own in memory dictionary.  Where TKEY is the type of the key (which is unique) and TVAL is the entity stored in the dictionary.

    What do you want to achieve? Just storing entities in a dictionary means that you have to parse each property in the entity each time you need to access the entity in the dictionary.

    Why don't you map the entity first to a custom class, from where you can use strong typed attributes on the class?

  • Verified answer
    Royal King Profile Picture
    27,686 on at

    try with below code

    var subjectRecords = myCRMProxy.RetrieveMultiple(query).Entities.ToArray();

    foreach (var subject in subjectRecords )

                               {

    dictionary.Add(subject .Id,subject.GetAttributeValue<string>("title"));

    }

  • Thom Profile Picture
    on at

    Hi Bas

    Thanks for the speedy response! Each dictionary is being called by a web service, which then updates a web form with options for a dropdownlists. My thinking was that mapping to a custom class would mean the development would not dynamically update when the system was.

    I'm new to this sort of development but had seem the <TKEY,TVAL> option mentioned on other forums, but was unable to decipher how I could apply it to our scenario.

  • Thom Profile Picture
    on at

    Hi Chitra

    Thank you for this. Have tried it out but got the following errors:

    - Argument1: cannot convert from 'System.Guid' to 'long'

    - The best overloaded method match for 'Systems.Collections.Dictionary<long,string)' has some invalid arguments

    Would this be down to the dictionary type we're using?

    Dictionary<Int64, string> dictionary = new Dictionary<Int64, string>();

  • Verified answer
    Royal King Profile Picture
    27,686 on at

    data type of id is guid not int so change the data type in dictionary

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

  • Thom Profile Picture
    on at

    Chitra, you are now my most favourite human being! Thanks so much for your input, prompt and direct. Much appreciated!!!

  • Bas van de Sande Profile Picture
    4,383 on at

    in case you want to learn more on generic programming, you can check out this simple example on www.dotnetperls.com/generic

    Generics can be very helpful :)

  • Thom Profile Picture
    on at

    Hi Bas, that's great - really useful resource, thanks very much!

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans