Dynamics CRM QueryExpression with LinkEntity - Stack Overflow

admin2025-04-17  6

I am using the following code

QueryExpression query = new ("account") {
    ColumnSet = new ColumnSet(
    "accountid",
    "name",
    "primarycontactid"
    ),
    Criteria = {
        Conditions = {
            new ConditionExpression("name", ConditionOperator.BeginsWith, "Cashmere")
        }
    }
};

LinkEntity le = query.AddLink(
    linkFromAttributeName: "primarycontactid",
    linkToEntityName: "contact",
    linkToAttributeName: "contactid",
    joinOperator: JoinOperator.Inner);

le.EntityAlias = "contact";
le.Columns = new ColumnSet("contactid","firstname","lastname","jobtitle");
var results = await _serviceClient.RetrieveMultipleAsync(query);

I have 3 results from the query all of which have a primarycontactid that exists in the Contact set however the link entity always returns 0. I have tried this with various different flavours and the link entity never has any results.

If I use a LINQ query, I am able to get both. What am I doing wrong with this QueryExpression.

I am using the following code

QueryExpression query = new ("account") {
    ColumnSet = new ColumnSet(
    "accountid",
    "name",
    "primarycontactid"
    ),
    Criteria = {
        Conditions = {
            new ConditionExpression("name", ConditionOperator.BeginsWith, "Cashmere")
        }
    }
};

LinkEntity le = query.AddLink(
    linkFromAttributeName: "primarycontactid",
    linkToEntityName: "contact",
    linkToAttributeName: "contactid",
    joinOperator: JoinOperator.Inner);

le.EntityAlias = "contact";
le.Columns = new ColumnSet("contactid","firstname","lastname","jobtitle");
var results = await _serviceClient.RetrieveMultipleAsync(query);

I have 3 results from the query all of which have a primarycontactid that exists in the Contact set however the link entity always returns 0. I have tried this with various different flavours and the link entity never has any results.

If I use a LINQ query, I am able to get both. What am I doing wrong with this QueryExpression.

Share Improve this question edited Feb 3 at 23:16 Henk van Boeijen 7,9686 gold badges35 silver badges43 bronze badges asked Feb 1 at 4:38 Alan RutterAlan Rutter 3314 silver badges19 bronze badges 2
  • the queryexpression looks correct to me, keep in mind that because you are not doing a LinkCriteria, the query should return only accounts where name begins with "Cashmere" that has a primarycontactid filled. If you can put a screenshot of an advanced find query will be helpful to understand – Guido Preite Commented Feb 1 at 22:42
  • You do not show the code that is processing the results. The contact fields will be returned as AliasedValue objects. Are you aware of that? – Henk van Boeijen Commented Feb 3 at 23:12
Add a comment  | 

1 Answer 1

Reset to default 0

As Henk pointed out the results are AliasedValues but what the Microsoft documentation for LinkEntity and all their examples don’t show is how the LinkEntity items are returned.

I stumbled across the answer. The results appear in your main entity results in the Attributes field. They use the entityAlias and field name as the key, so if your entityAlias is ‘leAlias’ and your column include Name then the attribute key will be ‘leAlias.name’.

Additionally be careful to specify filter criteria or you can end up with duplicate entities with varying attributes.

转载请注明原文地址:http://www.anycun.com/QandA/1744839523a88342.html