private static Guid AssociateN2N <TOne, TTwo>(LocalCrmDatabaseOrganizationService service,
                                                      EntityReference one, EntityReference two, Relationship relationship, DelayedException exception)
            where TOne : Entity
            where TTwo : Entity
        {
            var associationEntity = Activator.CreateInstance <N2NAssociation <TOne, TTwo> >();

            N2NAssociation <TOne, TTwo> .EntityLogicalName = relationship.SchemaName;
            associationEntity.LogicalName = N2NAssociation <TOne, TTwo> .EntityLogicalName;
            associationEntity.One         = one;
            associationEntity.Two         = two;

            _n2NAssociations.TryAdd(relationship.SchemaName, AssociationInfo.Create(typeof(N2NAssociation <TOne, TTwo>), associationEntity));
            return(Create(service, associationEntity, exception));
        }
        private static FilterExpression GetCriteriaWithMappedAssociations(FilterExpression fe, AssociationInfo info)
        {
            if (!fe.Conditions.Any(c => c.AttributeName == info.OneAttributeIdName || c.AttributeName == info.TwoAttributeIdName))
            {
                return(fe);
            }

            var newFilter = new FilterExpression(fe.FilterOperator)
            {
                ExtensionData = fe.ExtensionData,
#if !XRM_2013 && !XRM_2015 && !XRM_2016
                FilterHint = fe.FilterHint,
#endif
                IsQuickFindFilter = fe.IsQuickFindFilter
            };

            newFilter.Filters.AddRange(fe.Filters);

            foreach (var initialCondition in fe.Conditions)
            {
                var condition = initialCondition;
                if (condition.AttributeName == info.OneAttributeIdName)
                {
                    condition = new ConditionExpression(N2NAssociations.Fields.One, condition.Operator, condition.Values);
                }
                else if (condition.AttributeName == info.TwoAttributeIdName)
                {
                    condition = new ConditionExpression(N2NAssociations.Fields.Two, condition.Operator, condition.Values);
                }
                newFilter.AddCondition(condition);
            }

            return(newFilter);
        }