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)

Problem accessing data a RetrieveMultiple Query, likely a syntax problem

(0) ShareShare
ReportReport
Posted on by 1,589

I am trying to access a specific field in the first record that is returned to me via the RetrieveMultiple Query Expression code shown below.

However, I am getting the following error when building the code - 'Microsoft.Xrm.Sdk.Entity' does not contain a definition for 'new_licenseinfoId' and no extension method 'new_licenseinfoId' accepting a first argument of type 'Microsoft.Xrm.Sdk.Entity' could be found (are you missing a using directive or an assembly reference?)

The error is related related to the specific line of code immediately below.

 var mydata = entities[0].new_licenseinfoId;


As a newbie developer,  any help in fixing this so I can assign the specific value I need to the variable mydata would be appreciated.

The complete code that's relevant to this issue so you can see it in context is shown below.

COMPLETE CODE

        protected string DoRecsOfSpecifiedYearExist(IOrganizationService service, string year) 
        {
            QueryExpression query = new QueryExpression();
            query.EntityName = "new_s_licenseinformation";
            query.ColumnSet = new ColumnSet() { AllColumns = true };

            query.Criteria = new FilterExpression();
            query.Criteria.FilterOperator = LogicalOperator.And;
            query.Criteria.Conditions.Add
            (
                new ConditionExpression("new_licenseyear", ConditionOperator.Equal, year)
            );
            EntityCollection entities = service.RetrieveMultiple(query);

            var mydata = entities[0].new_licenseinfoId;
            //Error	2	'Microsoft.Xrm.Sdk.Entity' does not contain a definition for 'new_licenseinfoId' and no extension method 'new_licenseinfoId' accepting a first argument of type 'Microsoft.Xrm.Sdk.Entity' could be found (are you missing a using directive or an assembly reference?)	


            if (mydata != null)
            {
                string myYesResponse = "YES";
                return myYesResponse;
            }
            else
            {
                string myresponse = "NO";
                return myresponse;
            }
        }


*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Deepesh161 Profile Picture
    6,317 on at

    ntityCollection entityCollection = service.RetrieveMultiple(query);

               var mydata = entityCollection.Entities[0].new_licenseinfoId;

    I think you should do multiple null checks though in your code.

  • Verified answer
    Aiden Kaskela Profile Picture
    19,696 on at

    Hi Jim,

    You're getting the error because entities[0] is an Entity object and the Object doesn't have a property "new_licenseinfoId". You need to get the attribute from the object like this:

    if (entities[0].Contains("new_licenseinfoId"))

    {

    var theValue = entities[0]["new_licenseinfoId"]

    }

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

    Thanks,

     Aiden

    EDIT: The "Entity" class has a property "Attributes" which has all the values that are on your record. You can get an individual attribute from that collection like:

    myEntity.Attributes["my_field"] or shortcut with myEntity["my_field"]

  • Deepesh161 Profile Picture
    6,317 on at

    Aiden,

    Entity in the query above is a EntityCollection.

  • Aiden Kaskela Profile Picture
    19,696 on at

    Deepesh,

    The EntityCollection class allows you to access the entities as an indexed property of the class - that syntax is correct. The returned value is an Entity, and if you try to access a property on any class that doesn't exist you get a compile-time error that Jim described. If you look at the error, it's describing exactly what I said. You can copy the code any try for yourself if you aren't convinced.


    'Microsoft.Xrm.Sdk.Entity' does not contain a definition for 'new_licenseinfoId' and no extension method 'new_licenseinfoId' accepting a first argument of type 'Microsoft.Xrm.Sdk.Entity' could be found (are you missing a using directive or an assembly reference?)

    Regards,

      Aiden

  • Suggested answer
    Deepesh161 Profile Picture
    6,317 on at

    Exactly so what if the collection doesnt have anything returned

    the first thing to check is count of the collection?

  • Suggested answer
    Aiden Kaskela Profile Picture
    19,696 on at

    Deepesh,

    Yes, he should verify the collection size but that's not the problem here. If it were the problem that would return a null reference exception at runtime - not the error given at compile time. The error is a compile time error because the Entity class doesn't have a property called "new_licenseinfoid" - details: msdn.microsoft.com/.../microsoft.xrm.sdk.entity_properties.aspx

    Thanks,

    Aiden

    EDIT: It could also throw an index out of bounds exception depending on how the collection was accessed.

  • Verified answer
    Subramanya  Profile Picture
    165 on at

    Hi,

    Count the entity collection items, check for count greater then zero in your collection or try this

                       var entities = service.RetrieveMultiple(query).Entities;              

                         foreach(var item in entities)

                         {

                              var mydata = item.Attributes["new_licenseinfoId"];

                         }

  • ACECORP Profile Picture
    1,589 on at

    Thanks to all those who contributed to this question.

    Your responses got me over that hump and your input was greatly appreciated.

    If you are up for another challenge, I ran into a related issue that I posted here - https://community.dynamics.com/crm/f/117/t/180359

    Any help would be appreciated. I am trying to understand more about the variable of that variable when no records are returned.

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