private void AppendSelectFromRoot(string selectList = SearchValueConstants.SelectedFields, IReadOnlyList <IncludeExpression> includes = null) { _queryHelper.AppendSelect(selectList); if (includes?.Count > 0) { // add to the projection an array of {r1 _queryBuilder.AppendLine(","); _queryBuilder .Append("ARRAY(SELECT p.") .Append(SearchValueConstants.ReferenceResourceTypeName) .Append(", p.") .Append(SearchValueConstants.ReferenceResourceIdName) .Append(" FROM p in r.") .Append(KnownResourceWrapperProperties.SearchIndices).Append(" WHERE "); for (var i = 0; i < includes.Count; i++) { IncludeExpression includeExpression = includes[i]; if (i != 0) { _queryBuilder.Append(" OR "); } _queryBuilder.Append("("); if (includeExpression.WildCard) { _queryBuilder.Append("IS_DEFINED(p.").Append(SearchValueConstants.ReferenceResourceIdName).Append(")"); } else { _queryBuilder .Append("p.") .Append(SearchValueConstants.ParamName) .Append(" = '") .Append(includeExpression.ReferenceSearchParameter.Code) .Append("'"); } if (!string.IsNullOrEmpty(includeExpression.TargetResourceType)) { _queryBuilder .Append(" AND p.") .Append(SearchValueConstants.ReferenceResourceTypeName) .Append(" = '") .Append(includeExpression.TargetResourceType) .Append("'"); } _queryBuilder.Append(")"); } _queryBuilder.Append(") AS ").Append(KnownDocumentProperties.ReferencesToInclude); } _queryHelper.AppendFromRoot(); }