Post

3 followers Follow
0
Avatar

complex queries ((A or B) and C) etc

I would like to do a complex query with multipe conditions using And and Or.  Is this possible? I can't find any examples or documentation.

Example: (Submitted By Me or Assigned to Me) and Title contains some string

Import from old forum Answered

Please sign in to leave a comment.

5 comments

0
Avatar

The API allows you to nest AND and OR conditions in a tree like structure. For example, to implement the where condition you described above you will have to do something similiar to the below (Pseudo code.. don't expect it to compile):

 

            qry.Where = new And

            {

                Conditions = new Condition[]

                {

                    new Or

                    {

                        Conditions = new Condition[]

                        {

                            new Compare

                            {

                                    LeftExpression = new FieldExpression { FieldName = "Submitted" },

                                    Operator = Operator.Equal,

                                    RightExpression = ...

                            },

                            new Compare

                            {

                                    LeftExpression = new FieldExpression { FieldName = "AssignedTo" },

                                    Operator = Operator.Equal,

                                    RightExpression = ...

                            },

                        }

                    },

                    new Compare

                    {

                            LeftExpression = new FieldExpression { FieldName = "Manager" },

                            Operator = Operator.Equal,

                            RightExpression = new ConstantExpression{Value = "Some String"}

                    },

                }

            };

Clarizen Team 0 votes
Comment actions Permalink
0
Avatar

Just to better understand this syntax, how would you structure ((A or B) and (C or D))?

Dave Benjamin 0 votes
Comment actions Permalink
0
Avatar

Is this correct?

 

findCustomer.Where = new And

{

Conditions = new Condition[]

{

new Or

{

Conditions = new Condition[]

{

new Compare

{

LeftExpression = new FieldExpression { FieldName = "C_FirstName" },

Operator = Operator.Equal,

RightExpression = new ConstantExpression() { Value = new EntityId { TypeName = "Customer", Value = (string)FirstName.Value } }

},

new Compare

{

LeftExpression = new FieldExpression { FieldName = "C_LastName" },

Operator = Operator.Equal,

RightExpression = new ConstantExpression() { Value = new EntityId { TypeName = "Customer", Value = (string)LastName.Value } }

},

}

},

new Or

{

Conditions = new Condition[]

{

new Compare

{

LeftExpression = new FieldExpression { FieldName = "C_ContactNumber" },

Operator = Operator.Equal,

RightExpression = new ConstantExpression() { Value = new EntityId { TypeName = "Customer", Value = (string)ContactNumber.Value } }

},

new Compare

{

LeftExpression = new FieldExpression { FieldName = "C_EmailAddress" },

Operator = Operator.Equal,

RightExpression = new ConstantExpression() { Value = new EntityId { TypeName = "Customer", Value = (string)EmailAddress.Value } }

},

}

}

}

};

Dave Benjamin 0 votes
Comment actions Permalink
0
Avatar

Yes basically. The AndOr structure is correct.

However, the fields you used for the conditions (e.g. C_FirstName) are probably basic string fields (as opposed to Reference to Object fields), so the constant expressions on the Right expression shouldn't be of EntityId type, but plain strings (e.g.:

RightExpression = new ConstantExpression() { Value =  (string)FirstName.Value } )

 

Ophir Kenig 0 votes
Comment actions Permalink