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

时间:2015-03-13 14:53:01

标签: c# dynamics-crm

我正在尝试在C#for 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);

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

1 个答案:

答案 0 :(得分:0)

以下一行中有一个简单的拼写错误

lastNameFilter.Conditions = firstNameConditions.ToArray();

应该是

lastNameFilter.Conditions = lastNameConditions.ToArray();