将查询过滤器与 CRM FilterExpression 相结合

Combining query filters with CRM FilterExpression

我正在尝试在 C# 中为 CRM 4 编写以下(伪)查询:

Status = active 
AND 
(
    mail = somemail 
    OR 
    (
        firstName like firstNameSearchTerm 
        AND 
        lastName like LastNameSearchTerm
    )
)

问题是,中间名可能是 firstName 或 LastName 的一部分。我很难将其放入 ConditionExpressions / FilterExpressions。

#region mail conditions
            // Create the ConditionExpression.
            ConditionExpression mail1Condition = new ConditionExpression();
            mail1Condition.AttributeName = "emailaddress1";
            mail1Condition.Operator = ConditionOperator.Like;
            mail1Condition.Values = new object[] { Registration.Email };

            ConditionExpression mail2Condition = new ConditionExpression();
            mail2Condition.AttributeName = "emailaddress2";
            mail2Condition.Operator = ConditionOperator.Like;
            mail2Condition.Values = new object[] { Registration.Email };

            ConditionExpression mail3Condition = new ConditionExpression();
            mail3Condition.AttributeName = "emailaddress3";
            mail3Condition.Operator = ConditionOperator.Like;
            mail3Condition.Values = new object[] { Registration.Email };

            ConditionExpression statusCondition = new ConditionExpression();
            statusCondition.AttributeName = "statuscode";
            statusCondition.Operator = ConditionOperator.Equal;
            statusCondition.Values = new object[] { "1" };

            FilterExpression mailFilter = new FilterExpression();
            mailFilter.FilterOperator = LogicalOperator.Or;
            mailFilter.Conditions = new ConditionExpression[] { mail1Condition, mail2Condition, mail3Condition };

            #endregion mail conditions

            #region name conditions




            /* FIRST NAME */
            FilterExpression firstNameFilter = new FilterExpression();
            firstNameFilter.FilterOperator = LogicalOperator.Or;
            List<ConditionExpression> firstNameConditions = new List<ConditionExpression>();

            var firstAndMiddleNames = Registration.FirstName.Trim().Split(' ');
            firstAndMiddleNames = firstAndMiddleNames.Select(s => s.Replace(s, "%"+s+"%")).ToArray(); // Add wildcard search
            foreach (var item in firstAndMiddleNames)
            {
                ConditionExpression firstNameCondition = new ConditionExpression();
                firstNameCondition.AttributeName = "firstname";
                firstNameCondition.Operator = ConditionOperator.Like;
                firstNameCondition.Values = new object[] { item };
                firstNameConditions.Add(firstNameCondition);
            }
            firstNameFilter.Conditions = firstNameConditions.ToArray();

            /* LAST NAME */
            FilterExpression lastNameFilter = new FilterExpression();
            lastNameFilter.FilterOperator = LogicalOperator.Or;
            List<ConditionExpression> lastNameConditions = new List<ConditionExpression>();
            var lastAndMiddleNames = Registration.LastName.Trim().Split(' ');
            lastAndMiddleNames = lastAndMiddleNames.Select(s => s.Replace(s, "%" + s + "%")).ToArray(); // Add wildcard search
            foreach (var item in lastAndMiddleNames)
            {
                ConditionExpression lastNameCondition = new ConditionExpression();
                lastNameCondition.AttributeName = "lastname";
                lastNameCondition.Operator = ConditionOperator.Like;
                lastNameCondition.Values = new object[] { item };
                lastNameConditions.Add(lastNameCondition);
            }
            lastNameFilter.Conditions = firstNameConditions.ToArray();



            #endregion name conditions

            FilterExpression nameFilter = new FilterExpression();
            nameFilter.FilterOperator = LogicalOperator.And;                
            nameFilter.Filters = new FilterExpression[] { firstNameFilter, lastNameFilter };


            // Create the outer most filter to AND the state condition with the other filters
            FilterExpression stateFilter = new FilterExpression();
            stateFilter.FilterOperator = LogicalOperator.And;
            stateFilter.Conditions = new ConditionExpression[] { statusCondition };
            stateFilter.Filters = new FilterExpression[] { nameFilter };

            query.EntityName = EntityName.contact.ToString();
            query.Criteria = stateFilter;
            query.ColumnSet = columns;
            BusinessEntityCollection contacts = Service.RetrieveMultiple(query);

我 该查询目前绕过邮件过滤器以进行调试。 结果是它找到所有与名字或姓氏匹配的联系人(应该是 AND)。为什么???

下一行有一个简单的错字

lastNameFilter.Conditions = firstNameConditions.ToArray();

应该是

lastNameFilter.Conditions = lastNameConditions.ToArray();