top of page
Search
Writer's pictureSunil Thomas

Query expression Combining And & Or conditions




According to Microsoft, a query expression is a query written using query syntax. It is a fundamental language construct in C#, similar to any other expression, and can be used wherever a C# expression is valid. A query expression consists of several clauses written in a declarative syntax akin to SQL or XQuery. Each clause contains one or more C# expressions, which can themselves be query expressions or contain other query expressions.



Here's an example of a query expression:


In this example, we retrieve records from the "notes" table (annotations) that are associated with the "case" table. The notes records should either have the subject "Welcome letter" or "Confirmation Letter," and must meet additional conditions, such as having a creation date greater than the "caseAcceptedDate" in the "case" table. We use an 'or' condition to combine these criteria. The variable `filter1` represents the first filter condition, and `filter2` represents the second filter condition. We then combine these into a third filter condition, `filter3`, using the 'or' operator. Finally, we incorporate `filter3` into the query expression, add sorting, and retrieve the records.


var notesQry = new QueryExpression("annotation");

notesQry.ColumnSet = new ColumnSet("createdon", "documentbody", "filename"); var filter1 = new FilterExpression(LogicalOperator.And); filter1.AddCondition("objectid", ConditionOperator.Equal, caseReference.Id); filter1.AddCondition("createdon", ConditionOperator.GreaterEqual, caseAcceptedDate); filter1.AddCondition("subject", ConditionOperator.Equal, "Welcome letter");

var filter2 = new FilterExpression(LogicalOperator.And); filter2.AddCondition("objectid", ConditionOperator.Equal, caseReference.Id); filter2.AddCondition("createdon", ConditionOperator.GreaterEqual, caseAcceptedDate); filter2.AddCondition("subject", ConditionOperator.Equal, "Confirmation Letter"); var filter3 = new FilterExpression(LogicalOperator.Or);

filter3.AddFilter(filter1);

filter3.AddFilter(filter2);

notesQry.Criteria = filter3;

var createdOnAsc = new OrderExpression("createdon", OrderType.Ascending); notesQry.Orders.Add(createdOnAsc);

EntityCollection NotesRecords = service.RetrieveMultiple(notesQry);


0 views0 comments

Recent Posts

See All

Comments


bottom of page