private string BuildQuery() { Criteria contentTypeCriteria = Criteria.Eq("Content Type", "Computed", NewsContentType); Criteria dateCriteria = Criteria.Geq("Modified", "DateTime", "<Today OffsetDays='-7'/>"); List<Expression> domainCriteria = new List<Expression>(); List<Expression> audienceCriteria = new List<Expression>(); bool joinWithAnd = false; if (ddlCommunities.SelectedItem.Text != "All") { domainCriteria.Add(Criteria.Eq(CategoryDomains, "LookupMulti", ddlCommunities.SelectedItem.Text)); joinWithAnd = true; } else { CreateCriteria(EstablishedCommunities, CategoryDomains, ref domainCriteria); } if (ddlAudience.SelectedItem.Text != "All") { audienceCriteria.Add(Criteria.Eq(CategoryAudience, "LookupMulti", ddlAudience.SelectedItem.Text)); joinWithAnd = true; } else { CreateCriteria(EstablilshedAudiences, CategoryAudience, ref audienceCriteria); } Expression expression = null; Expression domainExpression = null; Expression audienceExpression = null; switch (domainCriteria.Count) { default: Operator domainOperator = new Or(); domainOperator.AddRange((IEnumerable<Expression>)domainCriteria); domainExpression = domainOperator; break; case 0: domainExpression = contentTypeCriteria; break; case 1: domainExpression = domainCriteria[0]; break; } switch (audienceCriteria.Count) { default: Operator audienceOperator = new Or(); audienceOperator.AddRange((IEnumerable<Expression>)audienceCriteria); audienceExpression = audienceOperator; break; case 0: audienceExpression = contentTypeCriteria; break; case 1: audienceExpression = audienceCriteria[0]; break; } if (joinWithAnd == true) { expression = domainExpression * audienceExpression; } else { expression = domainExpression + audienceExpression; } Or freeTextCriteria = null; //check free text if (!string.IsNullOrEmpty(txtSearch.Text)) { freeTextCriteria = Operator.Or( Criteria.Contains("Title", "Text", txtSearch.Text), Criteria.Contains("PublishingPageContent", "Note", txtSearch.Text) ); } if (freeTextCriteria != null) { expression = expression * freeTextCriteria; } if ((ddlCommunities.SelectedItem.Text == "All") && (ddlAudience.SelectedItem.Text == "All") && freeTextCriteria == null) { expression = dateCriteria * expression; } string result = String.Format("<Where>{0}</Where>", expression.GetCAML()); return(result); }