This documentation is archived and is not being maintained.

Using the FilterExpression Class

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Find the latest SDK documentation: CRM 2015 SDK

You can use the FilterExpession class to build a query that expresses: ( condition1 OR condition2 ) AND condition3, where:

Condition 1: Firstname = Joe

Condition 2: Firstname = John

Condition 3: City = Redmond

The following table lists the properties that you set to create a condition by using the ConditionExpression class.

ConditionsRepresents condition expressions that include attributes, condition operators, and attribute values.
FilterOperatorSpecifies logical AND/OR filter operators. This is set by using the LogicalOperator enumeration.
FiltersRepresents a hierarchy of condition and logical filter expressions that filter the results of the query.


The following code example shows how to use a FilterExpression where the city equals Redmond AND the first name is Joe OR John.

// Create the query expression and set the entity to contact.
QueryExpression query = new QueryExpression();
query.EntityName = "contact";

// Create a condition where the first name equals Joe.
ConditionExpression condition1 = new ConditionExpression();
condition1.AttributeName = "firstname";
condition1.Operator = ConditionOperator.Equal;
condition1.Values = new string[] { "Joe" }; 

// Create another condition where the first name equals John.
ConditionExpression condition2 = new ConditionExpression();
condition2 .AttributeName = "firstname";
condition2 .Operator = ConditionOperator.Equal;
condition2 .Values = new string[] { "John" }; 

// Create another condition where the city equals Redmond.
ConditionExpression condition3 = new ConditionExpression();
condition3 .AttributeName = "city";
condition3 .Operator = ConditionOperator.Equal;
condition3 .Values = new string[] { "Redmond" }; 

// Create a child filter to test for John OR Joe.
FilterExpression childFilter = new FilterExpression();
childFilter.FilterOperator = LogicalOperator.Or;
childFilter.Conditions = new ConditionExpression[] { condition1, condition2 };

// Create a parent filter to test for the city AND test for the child filter (first name).
FilterExpression topFilter = new FilterExpression();
topFilter.FilterOperator = LogicalOperator.And;
topFilter.Conditions = new ConditionExpression[] { condition3 };
topFilter.Filters = new FilterExpression[] { childFilter };

// Set the filter critera for the query to the complete filter expression.
query.Criteria = topFilter;

See Also

Other Resources

© 2010 Microsoft Corporation. All rights reserved.