Skip to main content



No record found.

Microsoft Dynamics CRM forum

How to compare a lookup ID with a Guid?

Posted on by 355

I created a CWA and need to compare two GUID: the first one is the id of the input parameter, the second one is the lookup id of a record retrieved with a query expression.

I'm retrieving both GUIDs by using the following instruction:


One of them (the id of the entity passed as an input parameter) is recognized as a GUID, the other one (the id of the related entity, i.e. the lookup id) is shown as an object {Microsoft.Xrm.Sdk.Reference}, so I can't compare them in the if statement to check whether they are equal or not.

As I need to check whether the related entity of the retrieved record is the same as the input one, I suppose I need to cast a lookup ID to GUID or to find another way to compare them.

This is the complete code, see line 19: while debugging, the variable tempCollectionId has the value {Microsoft.Xrm.Sdk.EntityReference}, whereas the variable myCollectionId has value {d4800847-7e4e-eb11-bf68-000d3a9c7067}:

public static void ExecuteBusinessLogic(IOrganizationService context, Entity myCollection)
                QueryExpression query = new QueryExpression("movie");
                query.ColumnSet = new ColumnSet(true);

                LinkEntity linkEntity = new LinkEntity("collection", "movie", "collectionid", "collectionid", JoinOperator.Inner);

                query.Distinct = true;
                List moviesInTheCollection = context.RetrieveMultiple(query).Entities.ToList();

                foreach (Entity movie in moviesInTheCollection)
                    var tempCollectionId = movie.Attributes["collectionid"];
                    var myCollectionid = mycollection.Attributes["collectionid"];

                    if (tempCollectionId == myCollectionid)
                        movie.Attributes["statecode"] = new OptionSetValue(1);

            catch (Exception ex)
                var message = $"Error:{ex.Message} - {ex.StackTrace}";
                throw new InvalidWorkflowException(message, ex);

(The general purpose is to test a CWA which will be called from the form of a specific collection to deactivate all the movies related to it)

  • Verified answer
    Pawar Pravin  Profile Picture
    Pawar Pravin 5,225 on at
    RE: How to compare a lookup ID with a Guid?

    Hi Joel,

    Please use below code to get lookup guid.

    Guid tempCollectionId = movie.Attributes.Contains("collectionid") ? movie.GetAttributeValue<EntityReference>("collectionid").Id : Guid.Empty;

  • Joel D Profile Picture
    Joel D 355 on at
    RE: How to compare a lookup ID with a Guid?

    Thank you for replying. How can I access the Id value?
    I'm trying to access it with the dot operator:

     var tempCollectionGuid = tempCollectionId.Id;

    but I can't get it this way because "Object does not contain a definition for 'Id'".

    I guess the problem is that the variable tempCollectionId is recognized as an Object (not an Entity Reference)...?

  • Suggested answer
    Pawar Pravin  Profile Picture
    Pawar Pravin 5,225 on at
    RE: How to compare a lookup ID with a Guid?

    In addition of Adrian suggestion, you can pass Guid parameter to filter your query expression so that explicitly you need not to filter recordcollection.

  • Suggested answer
    Adrian Begovich Profile Picture
    Adrian Begovich 20,973 Super User on at
    RE: How to compare a lookup ID with a Guid?

    Hi Joel D,

    Entity References contain a property called Id. The property value of the EntityReference.Id Property is a GUID representing the ID of the record. You will be able to compare this value against the other GUID value inside an if statement.

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .


André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,995 Super User

Martin Dráb Profile Picture

Martin Dráb 225,610 Super User

nmaenpaa Profile Picture

nmaenpaa 101,148


Featured topics

Product updates

Dynamics 365 release plans