Search code examples
c#dynamics-crmcrm

QueryExpression: How to compare the column values of linked entities?


Dynamics 365.

QueryExpression: How to compare the column values of linked entities?

var query1 = new QueryExpression("entity1");
query1.ColumnSet.AddColumns("userid");  
var link1 = query1.AddLink("entity2", "entity2id", "entity2Id", JoinOperator.Inner);
link1.LinkCriteria.AddCondition("ownerId", ConditionOperator.NotEqual, 
true, "entity1.userid"); // Here is problem!

Solution

  • Unfortunately this is not possible. It is only possible to compare columns that reside in the same the table/record.

    E.g. using this example:

    var query = new QueryExpression("account");
    query.Criteria.AddCondition(new ConditionExpression("createdon", ConditionOperator.Equal, true, "modifiedon"));
    

    ...you could query alle accounts that have never been modified. The true parameter in the constructor of the ConditionExpression simply tells the query processor to interpret the value part as a column name. However, you cannot specify the entity name as well.

    See MS Docs - Use column comparison in queries.