Search code examples
c#dynamics-crmquery-expressions

Dynamics CRM Query Expression filter or condition against two linked entities


I am currently trying to retrieve all the system users that are either assigned to a specific security role or are assigned to a team which has the security role. when building the query it seems only to filter them by an and condition when writing the query this way:

QueryExpression RolesQuery = new QueryExpression
        {
            EntityName = "systemuser",
            ColumnSet = new ColumnSet("systemuserid"),
            Distinct = true,
            Criteria =
            {
                Filters =
                {
                    new FilterExpression
                    {
                        FilterOperator = LogicalOperator.And,
                        Conditions =
                        {
                            new ConditionExpression("isdisabled", ConditionOperator.Equal, "0")
                        }
                    }
                }
            },
            LinkEntities =
            {
                new LinkEntity
                {
                    LinkFromEntityName = "systemuser",
                    LinkToEntityName = "systemuserroles",
                    LinkFromAttributeName = "systemuserid",
                    LinkToAttributeName = "systemuserid",
                    LinkCriteria =
                    {
                        Filters =
                        {
                            new FilterExpression
                            {
                                FilterOperator = LogicalOperator.And,
                                Conditions =
                                {
                                    new ConditionExpression("roleid", ConditionOperator.Equal, "00000000-0000-0000-0000-000000000000")
                                }
                            }
                        }
                    }
                },
                new LinkEntity
                {
                    LinkFromEntityName = "systemuser",
                    LinkToEntityName = "teammembership",
                    LinkFromAttributeName = "systemuserid",
                    LinkToAttributeName = "systemuserid",
                    LinkEntities =
                    {
                        new LinkEntity
                        {
                            LinkFromEntityName = "teammembership",
                            LinkToEntityName = "team",
                            LinkFromAttributeName = "teamid",
                            LinkToAttributeName = "teamid",
                            LinkEntities =
                            {
                                new LinkEntity
                                {
                                    LinkFromEntityName = "team",
                                    LinkToEntityName = "teamroles",
                                    LinkFromAttributeName = "teamid",
                                    LinkToAttributeName = "teamid",
                                    LinkCriteria =
                                    {
                                        Filters =
                                        {
                                            new FilterExpression
                                            {
                                                FilterOperator = LogicalOperator.And,
                                                Conditions =
                                                {
                                                    new ConditionExpression("roleid", ConditionOperator.Equal, "00000000-0000-0000-0000-000000000000")
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                        }
                    }
                }
            }
        };

My question: Is there a way to apply an or filter onto the two linked entities?

I have found a work around using two fetch expression queries then merging the two results and doing a distinct count but i am just curious to if there is a way in query expression to do it in one query.


Solution

  • No, it is not possible to wrap LinkEntity joins in a FilterExpression.

    In fact, with respect to link entities the filtering capabilities of the QueryExpression is rather limited. E.g. you cannot query records that have no related records of a specific entity type.